Javascript 无法在服务中访问资源响应对象
使用这种方法很有效Javascript 无法在服务中访问资源响应对象,javascript,angularjs,Javascript,Angularjs,使用这种方法很有效 angular.module('myApp.services', ['ngResource']). factory('Imgur', function($resource, $http) { $http.defaults.useXDomain = true; $http.defaults.headers.common['Authorization'] = 'Client-ID 123123123123'; var albumsService =
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;
});
-
{{album.title}
要在服务中使用相册
信息,请将回调传递给get()
:
执行
console.log
时,不执行异步getcompleted@plus-哦!你是对的!您能否提供一个小示例,说明我如何在factory函数中显示每个album.data.title
?
<ul class="nav nav-pills">
<li ng-repeat="album in imgur.albums().data">
{{album.title}}
</li>
</ul>
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);
});
});