My problem is really simple: I have a list of checkboxes like this:
<div class="form-group">
<label for="options">Options :</label>
<label *ngFor="#option of options" class="form-control">
<input type="checkbox" name="options" value="option" /> {{option}}
</label>
</div>
And I would like to send an array of the selected options, something like:
[option1, option5, option8]
if options 1, 5 and 8 are selected. This array is part of a JSON that I would like to send via an HTTP PUT request.
Thanks for your help!
Here's a simple way using
ngModel
(final Angular 2)I just faced this issue, and decided to make everything work with as less variables as i can, to keep workspace clean. Here is example of my code
Method, which called on change is pushing value in model, or removing it.
create a list like :-
Html :-
then in it's component ts :-
I have just simplified little bit for those whose are using list of value Object. XYZ.Comonent.html
** XYZ.Component.ts**.
create a list -- xyzlist.
Now to get value in Componenet.ts.
I have encountered the same problem and now I have an answer I like more (may be you too). I have bounded each checkbox to an array index.
First I defined an Object like this:
Then the checkboxes are like this:
As you know objects in JS are arrays with arbitrary indices. So the result are being fetched so simple:
Count selected ones like this:
And similar to that fetch selected ones like this:
You see?! Very simple very beautiful. TG.