簡體   English   中英

forEach AngularJS中的$ resource函數參數

[英]$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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM