Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 无法一次删除所有选中的对象_Angularjs - Fatal编程技术网

Angularjs 无法一次删除所有选中的对象

Angularjs 无法一次删除所有选中的对象,angularjs,Angularjs,我正在学习使用一种有角度的工具。我试图从数组中删除选中的对象,但并没有一次性删除所有对象。这是我的密码 <body ng-app="myApp"> <div ng-controller="myController"> [<a href="" ng-click="remove()">remove</a>] <ul> <li ng-repeat="user in users">

我正在学习使用一种有角度的工具。我试图从数组中删除选中的对象,但并没有一次性删除所有对象。这是我的密码

<body ng-app="myApp">

  <div ng-controller="myController">
    [<a href="" ng-click="remove()">remove</a>]
    <ul>
        <li ng-repeat="user in users">
            <input type="checkbox" ng-model="user.checked"/>
            {{user.name}}
        </li>
    </ul>  
  </div> 

  <script>
    var myApp = angular.module('myApp', []);
    myApp.controller('myController', function($scope){

      $scope.users = [
          {name:'John Smith', checked: false}, 
          {name: 'John Doe', checked: false}, 
          {name: 'Jane Doe', checked: false}, 
          {name:'Marry Jane', checked: false}        
      ]; 

      $scope.remove = function(){
          $scope.users.forEach(function(user){
              if(user.checked){
                  var index = $scope.users.indexOf(user);
                  $scope.users.splice(index, 1);
              }
          });
      };      

    });
  </script>
</body>    

[]
  • {{user.name}
var myApp=angular.module('myApp',[]); myApp.controller('myController',函数($scope){ $scope.users=[ {name:'John Smith',选中:false}, {name:'John Doe',选中:false}, {name:'Jane Doe',选中:false}, {姓名:'mary Jane',勾选:false} ]; $scope.remove=函数(){ $scope.users.forEach(函数(用户){ 如果(用户已选中){ var index=$scope.users.indexOf(用户); $scope.users.splice(索引1); } }); }; });
*注

我不知道密码里出了什么问题。我真的需要帮助

谢谢。

您可以使用过滤器:

您可以使用过滤器:


您可以通过如下方式删除:

$scope.remove = function(){
    $scope.users  = $scope.users.reduce(function(previousValue, currentValue){
     if(!currentValue.checked){
        previousValue.push(currentValue);
     }
     return previousValue;
  },[]);
};

您可以通过如下方式删除:

$scope.remove = function(){
    $scope.users  = $scope.users.reduce(function(previousValue, currentValue){
     if(!currentValue.checked){
        previousValue.push(currentValue);
     }
     return previousValue;
  },[]);
};

当您从用户数组中删除项时,数组更改和
forEach
循环不会按预期工作;因为原始数组在每次删除操作后都会发生更改

使用数组的过滤函数可以正确地完成工作

$scope.remove = function(){
    $scope.users = $scope.users.filter(function(user){
      return !user.checked;
    });
};      

当您从用户数组中删除项时,数组更改和
forEach
循环无法按预期工作;因为原始数组在每次删除操作后都会发生更改

使用数组的过滤函数可以正确地完成工作

$scope.remove = function(){
    $scope.users = $scope.users.filter(function(user){
      return !user.checked;
    });
};      

请注意,此答案有浏览器要求:FF 1.5和IE 9请注意,此答案有浏览器要求:FF 1.5和IE 9