如何使用angular.extend扩展对象?

时间:2014-07-10 01:19:29

标签: angularjs

我尝试使用多个ajax调用结果扩展angularJs中的对象。但是只有最后的结果扩展了我的空对象。

$rootScope.progress = {}; // the objkect to extend

$http.get('data.json').
then(function(result){angular.extend($rootScope.progress,result.data)}),

$http.get('error.json').
then(function(result){angular.extend($rootScope.progress,result.data)}),

$http.get('data2.json').
then(function(result){angular.extend($rootScope.progress,result.data)}),

$http.get('data3.json').
then(function(result)    {angular.extend($rootScope.progress,result.data)})

你可以在这里看到一个傻瓜:http://plnkr.co/edit/iDmsTpDpFUnvrv1pCUU9?p=preview

1 个答案:

答案 0 :(得分:1)

这是因为你的所有数据返回的是数组而不是对象。

将所有data.json更改为object。

请参阅http://plnkr.co/edit/Gq22dsksxlPYCryM1BOs?p=preview

如果你的json总是返回数组,那么你的$ scope.result应该改为数组。

您可以使用$scope.result = $scope.result.concat(value.data);

相关问题