5
  $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 函数是否可以访问/设置对象的上一个/下一个索引数组值,可能吗?

4

1 回答 1

7

是的,

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

不过,您需要防止索引超出数组末尾:

if(i < $scope.todos.length - 1) {
   $scope.todos[i+1].text = todo.text;
}
于 2013-02-07T19:16:08.043 回答