0

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?

1 Answer 1

1

Becasue you cannot use let local variable of angular2 inside formControl , you have to do like this to achive this

<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>
Sign up to request clarification or add additional context in comments.

3 Comments

I get a further error now that says: inline template:270:55 caused by: Cannot find control with unspecified name attribute
yes because the control name you provide dynamically is not exist anywhere in your component so throwing this error, i think you need to use formarray in this case
I don't quite understand. I am using a FormArray.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.