[英]$resource function parameters in forEach AngularJS
在此功能中,我从专辑列表中检索每个专辑:
options.albumsList.forEach(function (id) {
var promise = $resource('https://api.imgur.com/3/album/:album',{album:id},{
get:{
headers:{'Authorization':'Client-ID '+ options.apiKey},
method:'GET'
}
}).get().$then(
function(value){
albums.push(value);
},
function(err){
console.log(err);
}
);
prom.push(promise);
});
$q.all(prom).then(function () {
console.log(albums);
});
您会注意到的是:album
取决于albumList
每个id
这意味着如果我想将资源调用分配给可重用的对象,例如:
var call = $resource('https://api.imgur.com/3/album/:album',{album:id},{
get:{
headers:{'Authorization':'Client-ID '+ options.apiKey},
method:'GET'
}
});
然后用:
options.albumsList.forEach(function (album) {
//Can no longer pass album id to each call
var promise = call.get().$then(
function(value){
albums.push(value);
},
function(err){
console.log(err);
}
);
prom.push(promise);
});
$q.all(prom).then(function () {
console.log(albums);
});
我不能再将相册ID传递给每个:album
有没有解决的办法?
从资源文件 (向下滚动到返回段),貌似你可以传递一个参数来get
,将取代在URL中的价值。
var promise = call.get({album: album.id})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.