1

I've used .push() to create "List" input checked array for posting REST API. But it doesn't seem right.

When unchecking, the item in array is not removed automatically. Anybody have a better solution, pls help me! Tks

http://plnkr.co/edit/Y0YggxvVN1epIMWAdtiU?p=preview

1 Answer 1

1

You could do this... it works. Can't say this is the best solution though

 $scope.$watch('lists', function(lists){
    $scope.count = 0;
    angular.forEach(lists, function(list){
      if(list.checked){
        $scope.count += 1;
        if (inputsList.indexOf(list.id) == -1) {
            inputsList.push(list.id);
        };
      } else {
          inputsList.pop(list.id);
      }
    })
  }, true);

Same logic, but, modified a lil

index.html (added ng-click)

<input type="checkbox" name="list_id[]" ng-model="list.checked" value="{{list.id}}" ng-click='updateItem(list)' />

app.js (removed $scope.$watch and...)

$scope.currentSelectedItem = [];      
$scope.updateItem = function(item) {
    if(item.checked) {
        $scope.currentSelectedItem.push(item);
    } else {
        $scope.currentSelectedItem.pop(item);
    }   
}
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.