目前,我正在做这样的事情:
var albumList = $resource('https://api.imgur.com/3/account/guy123/albums').get(function () {
albumList.data.forEach(function (album) {
albums.push(album);
});
});
如何将其转换为可以在我的服务和控制器中调用的函数,例如:
factory('Imgur', function($resource, $http) {
var albumsService = {};
var albums = [];
albumsService.getAlbumList = function() {
var albumList = $resource('https://api.imgur.com/3/account/guy123/albums').get(function () {
albumList.data.forEach(function (album) {
albums.push(album);
});
});
};
albumsService.albumList = function() {
albumsService.getAlbumList();
return albums;
};
return albumsService;
});
.controller('Filters', ['$scope','Imgur', function($scope, Imgur) {
$scope.imgur = Imgur;
$scope.imgur.albumList();
//OR
$scope.imgur.getAlbumList();
//Some good context here is what if a user wanted to "refresh" the data.
$scope.updateFilter = function() {
$scope.imgur.getAlbumList();
};
}]);
最终目标是能够根据需要多次调用资源服务。服务应该是服务内部和控制器内部都可以调用的函数。