AngularJS – ForEach访问上一个/下一个索引数组,可能吗?
发布时间:2020-12-17 17:44:47 所属栏目:安全 来源:网络整理
导读:$scope.todos = [ {text:'learn',done:true},{text:'build',done:false},{text:'watch',{text:'destroy',{text:'rebuild',done:false}]; $scope.remaining = function() { var count = 0; angular.forEach($scope.todos,function(todo,i) { todo.text = 'wan
$scope.todos = [ {text:'learn',done:true},{text:'build',done:false},{text:'watch',{text:'destroy',{text:'rebuild',done:false}]; $scope.remaining = function() { var count = 0; angular.forEach($scope.todos,function(todo,i) { todo.text = 'want2learn'; todo.text[i+1] = todo.text; }); 是否有可能ForEach函数访问/设置对象的上一个/下一个索引数组值,可能吗? 解决方法
是,
$scope.todos[i+1].text = todo.text; 你想要防止索引超过数组的末尾: if(i < $scope.todos.length - 1) { $scope.todos[i+1].text = todo.text; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |