gpt4 book ai didi

javascript - 在服务中无法访问 Angular ngResource 响应对象

转载 作者:行者123 更新时间:2023-11-30 13:03:58 25 4
gpt4 key购买 nike

angular.module('myApp.services', ['ngResource']).
factory('Imgur', function($resource, $http) {

$http.defaults.useXDomain = true;
$http.defaults.headers.common['Authorization'] = 'Client-ID 123123123123';

var albumsService = {};

var albums = $resource('https://api.imgur.com/3/account/me123/albums').get();

//I can access albums.data in ng-repeate in view, but not here?
//returns undefined
console.log(albums.data);

albumsService.albums = function() {
return albums;
};

return albumsService;
});

使用这个有效

  <ul class="nav nav-pills">
<li ng-repeat="album in imgur.albums().data">
{{album.title}}
</li>
</ul>

最佳答案

为了在您的服务中使用专辑信息,将回调传递给get():

var albums = $resource('https://api.imgur.com/3/account/me123/albums').get(function () {
// Log out all the titles.
albums.data.forEach(function (album) {
console.log(album.title);
});
});

关于javascript - 在服务中无法访问 Angular ngResource 响应对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16361326/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com