Is this the angular way to keep track of checked items?

I have achieved what I want, which is to create an array of all checked items in angular. I plan to use these in another function.

My solution feels a bit awkward, is there a better way to do this?

<input 
    ng-repeat="task in $store.taskarr track by $index" 
    ng-change="checked(check,$index,task)" 
    ng-model="check" 
    type="checkbox" 
/>


$scope.checked = function(check,count,task){
    if(check){
        $scope.$store.checkedItems[count] = task;
    }else{
        delete $scope.$store.checkedItems[count];
    }
}

Answer

I liked this as it’s using less code:

        <input 
            ng-repeat="task in $store.taskarr track by $index" 
            ng-change="check ? ($store.checkedItems[$index] = task) :
                ($store.checkedItems[$index] = undefined)"
            ng-model="check" 
            type="checkbox" 
        />

Leave a Reply

Your email address will not be published. Required fields are marked *