Use Array.prototype.reduce
to check users before adding:
$scope.add = function(user){
var match = $scope.selectedUsers.reduce(function(prev, curr) {
return (user.id === curr.id) || prev;
}, false);
console.log(match ? 'match' : 'no match');
if (!match) $scope.selectedUsers.push(user);
};
... where your view looks like:
<p ng-repeat="user in users">{{user.username}} <a ng-click="add(user)">+</a></p>
To remove users from the selectedUsers
array, use Array.prototype.splice
:
$scope.remove = function(index){
$scope.selectedUsers.splice(index, 1)
}
...where your view looks like:
<p ng-repeat="selectedUser in selectedUsers">{{selectedUser.username}}
<a ng-click="remove($index)">--</a></p>