0

我正在使用 ReativeForms。我有一个数组,其中包含我想显示为复选框的值。这就是我所拥有的:

饮食限制清单包括

  • RestrictionType: string = 应该是复选框的名称
  • IsChecked: boolean = 是否检查

在我的 ngOnInit() 中,我初始化了我的数组。

this.healthInfoForm = this._fb.group(
{
    dietaryRestrictionList: this._fb.array([]),
});

当我得到数据时,我做了一个 for 循环,我设置了值:

> const control =
> <FormArray>this.healthInfoForm.controls['dietaryRestrictionList']; 
> for(var i = 0; i < this.dietaryRestrictionList.length; i++){
>     let checkBoxLabel = this.dietaryRestrictionList[i].RestrictionType;  
> control.push(this._fb.group({
>         checkBoxLabel: this.dietaryRestrictionList[i].IsChecked// set whether it's checked or not
>     }))   }

现在我想在 html 页面中显示这个:

        <div formArrayName="dietaryRestrictionList" class="form-group">
            <div *ngFor="let diet of healthInfoForm.controls.dietaryRestrictionList.controls; let i=index" > 
                <div [formGroupName]="i">                               
                  <label>
                      <input type="checkbox" [formControl]="let diet of healthInfoForm.controls.[diet.boxName]" class="form-control">                              
                  </label>  
                </div>  
            </div>
        </div>

我正在尝试遵循此示例:https ://scotch.io/tutorials/angular-2-form-validation

事情不工作。我收到一条错误消息:

Unhandled Promise rejection: Template parse errors:
Parser Error: Unexpected token let at column 1 in [let diet of 
        healthInfoForm.controls.[diet.boxName]] in HealthInformationComponent@270:53 ("          
    <label><input type="checkbox" [ERROR ->][formControl]="let diet of healthInfoForm.controls.[diet.boxName]" class="form-control">"): HealthInformationComponent@270:53

我怎样才能让它工作?

4

1 回答 1

1

因为你不能let在里面使用 angular2 的局部变量formControl,你必须这样做才能实现这一点

<div formArrayName="dietaryRestrictionList" class="form-group">
    <div *ngFor="let diet of healthInfoForm.controls.dietaryRestrictionList.controls; let i=index" > 
        <div [formGroupName]="i">                               
          <label>
              <input type="checkbox" [formControl]="diet[i]" class="form-control">
          </label>  
        </div>  
    </div>
</div>
于 2017-03-06T10:14:44.060 回答