Currently, I'm doing things like this:
var albumList = $resource('https://api.imgur.com/3/account/guy123/albums').get(function () {
albumList.data.forEach(function (album) {
albums.push(album);
});
});
How do I turn that into a function that I can call in both my service and controller like:
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();
};
}]);
Ultimately the goal here is to be able to call a resource service as many times as I want. The service should be a function callable by both inside the service and inside the controller.
$scope.imgur.albumList();doesn't make sense. You're not assigning the result of the function to anything.var albumListas a function. Ultimately the goal here is to be able to call a resource service as many times as I want. The service should be a function callable by both inside the service and inside the controller.