2013-02-07 15 views
4
$scope.todos = [ 
    {text:'learn', done:true}, 
    {text:'build', done:false}, 
    {text:'watch', done:false}, 
    {text:'destroy', done:false}, 
    {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函數是否可以訪問/設置對象的前一個/下一個索引數組值,可能嗎?AngularJS - ForEach訪問上一個/下一個索引數組,可能嗎?

回答

5

是,

$scope.todos[i+1].text = todo.text; 

你要警惕索引過去數組的最後階段:

if(i < $scope.todos.length - 1) { 
    $scope.todos[i+1].text = todo.text; 
} 
相關問題