相关疑难解决方法(0)

Angular 2.如何在Reactive Forms中使用控件对象数组

我需要动态创建textarea表单.我有以下模型:

this.fields = {
      isRequired: true,
      type: {
        options: [
          {
            label: 'Option 1',
            value: '1'
          },
          {
            label: 'Option 2',
            value: '2'
          }
        ]
      }
    };
Run Code Online (Sandbox Code Playgroud)

形式:

this.userForm = this.fb.group({
      isRequired: [this.fields.isRequired, Validators.required],
      //... here a lot of other controls
      type: this.fb.group({
         options: this.fb.array(this.fields.type.options),
      })
});
Run Code Online (Sandbox Code Playgroud)

模板的一部分:

<div formGroupName="type">
       <div formArrayName="options">
         <div *ngFor="let option of userForm.controls.type.controls.options.controls; let i=index">
            <textarea [formControlName]="i"></textarea>
         </div>
      </div>
</div>
Run Code Online (Sandbox Code Playgroud)

所以,你可以看到我有一些对象,我想使用label属性来显示它textarea.现在我明白了[object Object].如果我options改为简单的字符串数组,如:['Option 1', 'Option 2'] …

forms angular

13
推荐指数
2
解决办法
4万
查看次数

标签 统计

angular ×1

forms ×1