Vivek V Dwivedi Vivek V Dwivedi - 1 year ago 73
AngularJS Question

How to define a model for dynamically created checkboxes in angular

I am creating a table of users where I want to add a checkbox on each row and a delete button. When I click the delete button, I want to delete all users who were selected.

Now I am creating these user entries from an API response which gives me say id, name and email.

So my view looks something like this:

<tr ng-repeat="user in track by $index">
<td><input type="checkbox"></td>

What I want in my controller is to have an object with id of all the users for whom the checkbox was clicked.

Even if I create an object and assign it as model for checkbox, how do I add a key as id in that object?

Answer Source

You could simply do <input type="checkbox" ng-model="user.isSelected">

And then just filter for those that have isSelected === true

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download