相关疑难解决方法(0)

Angular2 - 动态FormArray复选框

我在angular2和typescript中构建了一个Form.我尝试动态添加一个复选框列表,这对我不起作用,我的错误在哪里?

模板代码:

<div formArrayName="categories">
   <div class="form-group" *ngFor="let category of updateDetailsForm.controls.categories.controls; let i = index">
      <label>
        <input type="checkbox" formControlName="{?{i}}">
          {?{category.name}}
      </label>
   </div>
</div>
Run Code Online (Sandbox Code Playgroud)

Typescript代码:

updateDetailsForm: FormGroup;
private categories : Category[] = []

constructor(private router: Router,
            private ss : SearchService,
            private formBuilder: FormBuilder)
{
   this.initForm() // before the categories loaded
   this.ss.getAllCategories().subscribe(
     data => {
       this.categories = data
       this.initForm() // refresh the form with categories
     }
   )
 }

initForm() {
  let allCategories: FormArray = new FormArray([]);
  for (let i = 0; i < …
Run Code Online (Sandbox Code Playgroud)

forms checkbox typescript angular

5
推荐指数
1
解决办法
1万
查看次数

标签 统计

angular ×1

checkbox ×1

forms ×1

typescript ×1