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

4

1 回答 1

1

你可以这样做......它有效。不能说这是最好的解决方案

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

相同的逻辑,但是修改了一个 lil

index.html(添加了 ng-click)

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

app.js(删除了 $scope.$watch 和...)

$scope.currentSelectedItem = [];      
$scope.updateItem = function(item) {
    if(item.checked) {
        $scope.currentSelectedItem.push(item);
    } else {
        $scope.currentSelectedItem.pop(item);
    }   
}
于 2013-06-15T04:30:39.820 回答