【发布时间】:2015-04-17 23:36:03
【问题描述】:
我对 AngularJS 的一个问题是,我无法在 $http.get 方法中为外部变量赋值。这是我的代码 sn-p:
.factory('MediaService',['$http', function($http, $q){
return {
getGalleryIds : function() {
galeriURL = 'http://api.tika.gov.tr/www/tr/galleries?';
return $http.get(galeriURL, { cache: true}).then(function(response){
contentIdList = response.data.data;
var contentList = [];
for (i=0;i<contentIdList.length;i++) {
var galeriContentURL = 'http://api.tika.gov.tr/www/tr/galleries/' + contentIdList[i].id;
contentList[i] = $http.get(galeriContentURL, { cache: true})
.then(function(response){
return response.data;
});
}
console.log(contentList);
return contentList;
});
}
}
}]);
我的问题是在 console.log(contentList); 行我得到 Promise Array 因为我无法为外部变量赋值。
[Promise, Promise, Promise, Promise, Promise, Promise, Promise, Promise, Promise, Promise]
如何在 for loop $$http.get 和行 console.log(contentList) 中为 var contentList = [ ]; 变量赋值; 得到对象数组如下
[Object, Object, Object, Object, Object, Object, Object, Object, Object, Object]
【问题讨论】:
标签: angularjs http get angularjs-service angular-promise