I'm using ReativeForms. I have an array with values that I would like to display as checkboxes. This is what I have:
dietaryRestrictionList consists of
- RestrictionType: string = which should be the name of the checkbox
- IsChecked: boolean = whether it's checked or not
In my ngOnInit() I initialize my array.
this.healthInfoForm = this._fb.group(
{
dietaryRestrictionList: this._fb.array([]),
});
When I get the data I do a for loop I set the values:
> 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
> })) }
Now i want to display this in the html page:
<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>
I'm trying to follow this example:https://scotch.io/tutorials/angular-2-form-validation
Things are not working. I get an error that says:
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
how can I get this to work?