Is there a way to fetch data from a service before the view and controller are loaded? I need assistance with this.
resolve: {
getAlbum: function(albumService){
return albumService.getAlbums();
},getAtum: function(albumService){
return albumService.getAtum();
}
}
I am working with a service that looks like this:
.factory('albumService', ['$http', '$q'],
function albumService($http, $q) {
// interface
var service = {
albums: [],
getAlbums: getAlbums,
getAtum:getAtum
};
return service;
// implementation
function getAlbums() {
var def = $q.defer();
$http.get("data.json")
.success(function(data) {
service.albums = data;
def.resolve(data);
})
.error(function() {
def.reject("Failed to get albums");
});
return def.promise;
}
function getAtum() {
var def = $q.defer();
$http.get("data.json")
.success(function(data) {
service.albums = data;
def.resolve(data);
})
.error(function() {
def.reject("Failed to get albums");
});
return def.promise;
}
})
I want to receive data from the service before initializing the controller and view.
.controller('PlaylistsCtrl', function($scope) {
$scope.playlists = [{
title: 'Reggae',
id: 1
}, {
title: 'Chill',
id: 2
}, {
title: 'Dubstep',
id: 3
}, {
title: 'Indie',
id: 4
}, {
title: 'Rap',
id: 5
}, {
title: 'Cowbell',
id: 6
}];
})
I am fetching data from two services and would like to display a loader until both sets of data are retrieved. To achieve this, I am using:
http://ionicframework.com/docs/api/service/$ionicLoading/