I just started using angular and I'm facing an issue with my user route. I'm trying to resolve the user object before rendering the view, but even after injecting $q and deferring the promise, the view is loading before the promise gets returned.
Here's how my Route looks like:
.when('/user/:userId', {
templateUrl: 'user/show.html',
controller: 'UserController',
resolve: {
user: userCtrl.loadUser
}
})
And this is what my Controller code looks like:
var userCtrl = app.controller('UserController', ['$scope',
function($scope){
$scope.user = user; // The User object is undefined
// This console log fires before the user object is resolved
console.log("Firing from the controller");
}]);
userCtrl.loadUser = ['Restangular', '$route', '$q',
function(Restangular, $route, $q) {
var defer = $q.defer();
Restangular.one('users', $route.current.params.userId).get().then(function(data) {
console.log("Firing from the promise");
defer.resolve(data);
});
return defer.promise;
}];