This is how I work with the solution. He must receive a promise. Therefore, I create the service accordingly.
app.factory('User', function($http){ var user = {}; return { resolve: function() { return $http.get('api/user/1').success(function(data){ user = data; }); }, get: function() { return user; } } });
This is the main idea. You can also do something similar with $q
app.factory('User', function($q, $http){ var user = {}; var defer = $q.defer(); $http.get('api/user/1').success(function(data){ user = data; defer.resolve(); }).error(function(){ defer.reject(); }); return { resolve: function() { return defer.promise; }, get: function() { return user; } } });
They are almost identical in action. The difference is that in the first case, the service will start receiving the date when you call the resolve() service method, and in the second example, it will start fetching when creating the factory object.
Now in your condition.
$stateProvider.state('app.index', { url: '/me', templateUrl: '/includes/app/me.jade', controller: function ($scope, $rootScope, User) { $scope.user = User.get(); console.log($scope.user); }, controllerAs: 'vm', resolve: { auser: function(User) { return User.resolve() } } });
source share