javascript – 在AngularJS中编写待办事项列表时无法理解删除过程

我试图在 Angularjs中编写一个todo应用程序,代码如下.

function write_controller($scope){
  $scope.todos = [{text:'hey all',done:false},{text:'hello',done:false}];
  $scope.addtodo = function(){
  $scope.todos.push({text:$scope.todopush,done:false});
    $scope.todopush = '';
  }
  $scope.delete = function(){
    var oldtodos = $scope.todos;
    $scope.todos = [];
    angular.forEach(oldtodos,function(x){
      if (!x.done){
        $scope.todos.push(x);}
    });
  };
}
<html ng-app>
    <head>
     <script src =  
     "https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js">   
      </script>
      </head>
      <body>
      <div class = "basic_class" ng-controller = "write_controller">
      <ul>
        <li ng-repeat = "todo in todos"><input type = "checkBox" ng-model  
         ="todo.done">{{todo.text}}</input></li>
      </ul>
      <input type = "text"
        placeholder = "please add data here"
        ng-model="todopush">
      </input>
      <input type = "button" ng-click ="addtodo()" value ="Click me!!"></input>
      <input type = "button" ng-click = "delete()" ng-model="remove" value =  
       "remove!"></input>
      </div>
</body>
</html>

在下面的代码中,我有以下问题.
删除操作如何工作?

我的理解:
1)它将现有数组推送到新数组中
2)它清除exisitng数组
3)迭代新数组
3)检查完成的功能??? (当它说!x.done时,是真还是假???)
任何帮助将受到高度赞赏.

解决方法

走过台阶

$scope.delete = function() {
    // store existing todos array in variable
    var oldtodos = $scope.todos;
    // create new empty array for todos
    $scope.todos = [];
    // loop over prevIoUsly stored todos
    angular.forEach(oldtodos,function(x) {//x is element of the array - a single todo object
      // if x.done is not truthy
      if (!x.done){
       // push this item into `$scope.todos`
       $scope.todos.push(x);
    });
}

基本上它只将完成:false推送到$scope.todos的新版本,它有效地删除了done:true

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...