mardi 18 octobre 2016

Angular 2 - binding checkboxes created by ngFor to a boolean array

I am dynamically creating a dropdown list of items using ngFor, looping through a string array (obtained from an SQL query), saved within a service.

<li *ngFor="let vehicleClasses of getVehicleClassList(); let i = index" (click)="toggleCheckBox(i)">
  <a>
    <input type="checkbox" id="vehCB_"/>
  </a>
</li>

The dropdown menu has a checkbox and then the name of the selection (. How do I store which checkboxes the user has checked (and preferably from within the service rather than the component)?
Ideally, I want to have a boolean array which store the checkbox value. If it is possible, can I then bind the checkbox to a value so if, for example, a function were to change all the checkbox booleans, the checkboxes would update?

I've seen ngModel used in other similar scenarios - but I have not been able to successfully get it working.




Aucun commentaire:

Enregistrer un commentaire