简体   繁体   中英

Create List input checked array for posting REST API

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

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);
    }   
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM