mercredi 20 septembre 2017

How to get the checked item inside a ng-repeat using angular?

I want to get the check item from a list withing a ng-repeat in angular. Once the item is checked I want to put that checked item to another list.Here is my code so far.

 <div class="col-lg-12" data-ng-repeat="user in users track by $index">
    <div class="col-lg-12">
        <div class="col-lg-3">  </div>
        <div class="col-lg-3">
            <input type="checkbox" data-ng-checked="selectUser(user)" data-ng-model="user.isSelected" />            
        </div>
    </div>
</div>

 <div class="col-lg-12" data-ng-repeat="selectedUser in selectedUsers track by $index">
    <div class="col-lg-12">
        <div class="col-lg-3">  </div>
        <div class="col-lg-3">                  
        </div>
    </div>
</div>

This is my controller function to get the checked users.

$scope.selectUser = function(user){
    if (user.isSelected) {
        if ($scope.selectedUsers.indexOf(user.id) === -1) {
            $scope.selectedUsers.push(user);
        }
    }else {
        var index = $scope.selectedUsers.indexOf(user.id);
        if ($scope.selectedUsers.indexOf(user.id) != -1) {
            $scope.selectedUsers.splice(index, 1);
        }

} When I check a checkbox, all the users value will be passed to selectUsers() function. And it will give incorrect result. I want only to get the selected users. How can I do this?




Aucun commentaire:

Enregistrer un commentaire