My application needs some basic data. This is why I created the service, and I use it as a model for this shared data so that it can be accessed by all controllers. I am trying to enable this service using the ui route redirection function, which says that if I return the promise, it will be resolved before the controller starts, but it does not work for me. here is my code
service:
var Data = function ($q, $http) {
var list = {};
var cachedData;
var resolveData;
resolveData = function () {
return $http.get('/api/data')
.then(function (response) {
var deferred = $q.defer();
deferred.resolve(list.setData(response.data));
return deferred.promise;
}, function (response) {
});
};
list.getData = function () {
if (cachedData) {
return cachedData;
}
else {
resolveData();
}
};
list.setData = function (data) {
cachedData = data;
return data;
};
return list;
};
Data.$inject = ['$q', '$http'];
Route:
.state('temp', {
url: 'temp',
templateUrl: '/temp',
controller: 'temp',
resolve: {
data: function (data) {
return data.getData();
}
}
})
Controller:
var temp = function(data, $scope){
console.log('asad');
$scope.showLoading = true;
$scope.prefixes = data.something;
$scope.lists = data;
};
temp.$inject = ['data', '$scope'];
source
share