I have created a page where users can select/ unselect employees. I am using .push to add these employees to an array, users. I am also using .splice to remove the unchecked item from the array. Currently, unchecking the last checked item works fine, however, if I select multiple items and uncheck the first item displayed in the view, it will remove every item from the array.
Here is the HTML:
<div ng-app="myApp">
<div ng-controller="MainCtrl">
<table>
<tr ng-repeat="user in users">
<td><input type="checkbox" ng-model="checked" ng-change="addremoveuser(checked, user.ID)"><td>
<td>{{user.Title}}</td>
<td>{{user.FirstName}}</td>
<td>{{user.LastName}}</td>
</tr>
</table>
</div>
</div>
Here is the JS:
var app = angular.module('myApp', ['ngSanitize']);
app.controller('MainCtrl', function($scope, $http, $q){
var users = [];
$scope.addremoveuser = function (checked, id) {
if (checked) {
console.log("user selected", id);
users.push(id)
console.log("if test", users);
}
else {
console.log("user unselected", id);
var index = users.indexOf(id);
users.splice(index);
console.log("else test", users);
}
};
});
How can I fix this to only remove the item that was unchecked?
addremoveuser(checked, $index)->users.splice(id);