Bind checkbox to value instead of true / false with reactive forms

I am trying to get a simple example of work using reactive / model forms. I want to bind an array of flags to a list of values, not a list of boolean values.

This plunker shows the problem: http://plnkr.co/edit/a9OdMAq2YIwQFo7gixbj?p=preview The values ​​associated with the forms are logical, I would like the answer to be something like ["value1", "value2", etc]

I have something like this, but I don’t know how to get the result I want?

Template:

 <form [formGroup]="checkboxGroup"> <input *ngFor="let control of checkboxGroup.controls['myValues'].controls" type="checkbox" id="checkbox-1" value="value-1" [formControl]="control" /> </form> 

And component:

 let checkboxArray = new FormArray([ new FormControl({checked: true, value: "value1"}), new FormControl({checked: false, value: "value2"}))]); this.checkboxGroup = _fb.group({ myValues: checkboxArray }); 
+5
source share
1 answer

The flag value is either checked (true) or not checked (false). Generally speaking, it doesn't make sense that the checkbox has a different meaning, but if you want to do this, however, you will need to write your own evaluative argument.

+1
source

Source: https://habr.com/ru/post/1261100/


All Articles