使用angularjs在数组中保存值

使用angularjs在数组中保存值,angularjs,Angularjs,下面的代码有什么问题 var activity; angular.forEach(planning, function(value,key){ activity = value.data $scope.registration.push( {"uuid": activity.id, "type": activity.type } ); } 所有注册数组的字段都等于最后一个值。最后,在

下面的代码有什么问题

var activity;
    angular.forEach(planning, function(value,key){
       activity = value.data
       $scope.registration.push(
         {"uuid": activity.id,
         "type": activity.type
         }
       );
    }

所有注册数组的字段都等于最后一个值。

最后,在循环函数中声明活动解决了我的问题

angular.forEach(planning, function(value,key){
   var activity;
   activity = value.data
   $scope.registration.push(
     {"uuid": activity.id,
     "type": activity.type
     }
   );
}

虽然在迭代器函数中声明
activity
是一个好主意,但在这里没有什么区别,因为angular以同步和顺序调用迭代器。您的
计划
变量一定出了问题。你能描述一下它是从哪里来的吗?我看没问题,你能检查一下这个plnkr吗。我已经根据代码的预期构造了计划变量,所以请检查它。