3

创建由以下“嵌套对象”(建筑物)组成的嵌套表单:

export class Building{
  id: number = 0;
  doorsCount: number = 0;
  description: string = '';
  address: Address = new Address();
  buildingType: BuildingType = new BuildingType();
}

export class Address{
  id: number = 0;
  description: string = '';
}

export class BuildingType{
  id: number = 0;
  description: string = '';
}

如您所见,Building 类包含其他类,例如 Address 和 BuildingType,它们还具有其他属性,例如 id 和 description。

创建表单时,我在组件 ts 文件中使用了以下代码:

buildingForm: FormGroup;

construct(private fb: FormBuilder){
 this.buildingForm = this.createBuildingFG(new Building);
}

createBuildingFG(building: Building){
  let formGroup : FormGroup;
  formGroup = this.fb.group(building);

  // Because object of type "building" contain properties of non-primitive 
  // types such as object Address and BuildingType I think the following 
  // additional lines are required. 

  formGroup.controls.address = this.fb.group(building.address);
  formGroup.controls.buildingType = this.fb.group(building.buildingType);

  return formGroup;
}

这就是表单绑定到 HTML 模板的方式:

<form [formGroup]="buildingForm">
    <label>
        Door count: 
        <input formControlName="doorsCount" >
    </label>
    <label>
        Building description: 
        <input formControlName="description" >
    </label>
    <label formGroupName="address">
        Address: 
        <input formControlName="description"  >
    </label>
    <label formGroupName="buildingType">
         Building type: 
        <input formControlName="description" >
    </label>    
</form>

现在,当我输出整个表单的值时,问题就出现了,这些值并没有真正根据在 formGroup 内的嵌套字段控件中键入的内容进行更新,例如地址或 buildingType。否则正常更新。

这是输出值的方式

<div>
   {{buildingForm.value | json}}
</div>

但是,如果 createBuildingFG 函数的执行方式不同,并且每个 formControl 都是显式创建的,而没有传递整个对象,则表单会正常运行。例子:

createBuildingFG(building: Building){
  let formGroup : FormGroup;
  formGroup = this.fb.group({
    doorsCount: '',
    description: '',
    address: this.fb.group({ description: ''}),
    buildingType: this.fb.group({ description: ''})
  });

  return formGroup;
}

任何人都可以解释发生了什么?显然,为了避免执行显式定义 fromGroup 的每个元素的繁琐任务,人们可能只想传递整个对象。

4

1 回答 1

3

正如评论中提到的@jonrsharpe

因为最初的构造会做您随后不会覆盖的事情。

那么是什么东西呢?

当角度创建 FormGroup 的新实例时,它会调用_setUpControls方法

export class FormGroup extends AbstractControl {
  constructor(
      public controls: {[key: string]: AbstractControl},
      validatorOrOpts?: ValidatorFn|ValidatorFn[]|AbstractControlOptions|null,
      asyncValidator?: AsyncValidatorFn|AsyncValidatorFn[]|null) {
    super(
        coerceToValidator(validatorOrOpts),
        coerceToAsyncValidator(asyncValidator, validatorOrOpts));
    this._initObservables();
    this._setUpdateStrategy(validatorOrOpts);
    this._setUpControls();  <--------------

现在让我们看看方法:

/** @internal */
_setUpControls(): void {
  this._forEachChild((control: AbstractControl) => {
     control.setParent(this);
     control._registerOnCollectionChange(this._onCollectionChange);
  });
}

正如我们所看到的,每个控件项都设置了父项并注册了一些事件,但您不这样做。

以下代码应该可以工作:

formGroup = this.fb.group(building);
formGroup.controls.address = formGroup.controls.buildingType = null;
formGroup.registerControl('address', this.fb.group(building.address));
formGroup.registerControl('buildingType', this.fb.group(building.buildingType));

或者您可以使用递归来完成它的工作:

constructor(private fb: FormBuilder){
  this.buildingForm = this.createFormGroup(new Building);
}

createFormGroup(obj: any) {
  let formGroup: { [id: string]: AbstractControl; } = {};

  Object.keys(obj).forEach(key => {
    formGroup[key] = obj[key] instanceof Object ? this.createFormGroup(obj[key]) : new FormControl(obj[key]);
  });

  return this.fb.group(formGroup);
}
于 2017-08-14T04:51:17.923 回答