无法将formGroup添加到FormArray

时间:2018-10-04 07:11:17

标签: angular forms ionic2 ionic3

我无法将Angular Reactive Form组添加到FormArray。请参见下面的代码。

Home.ts

ngOnInit(){
    this.myForm = this.formBuilder.group({
      items: this.formBuilder.array([])
    });
    this.addItemsToForm(); 

    console.log("form----- "+JSON.stringify(this.myForm.value))
  }

addItemsToForm(){
    const arrayControl = <FormArray>this.myForm.get('items')
    arrayControl.push(
      this.formBuilder.group({
        hazardStatus: ["dsad"],
      })
    )
  } 

Home.html

<form [formGroup]="myForm">
  <div formArrayName="items">

   <div *ngFor="let model of items; let k = index;" no-padding style="background-color:transparent">
      {{items.length}}
      <div [formGroupName]="k">
          {{items.length}}
          <ion-item>
              <ion-label item-left stacked>
                  {{model.hazardStatus}}
              </ion-label>
          </ion-item>
      </div>
    </div>
  </div>
</form>

我的日志显示表单值{"items":[{"hazardStatus":"dsad"}]}

但是,表单没有显示。

0 个答案:

没有答案