Question

I'm new to angular and I have a user route which I'm attempted to resolve the user object for before rendering the view. I've injected $q and deferred the promise, however, the view is still loading before the promise is returned.

Route:

.when('/user/:userId', {
    templateUrl: 'user/show.html',
    controller: 'UserController',
    resolve: {
        user: userCtrl.loadUser
    }
})

Controller

var userCtrl = app.controller('UserController', ['$scope',
    function($scope){
        $scope.user = user; // User is undefined

        // This fires before the user is resolved
        console.log("Fire 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("Fire from the promise");
            defer.resolve(data);
        });

        return defer.promise;
    }];
Was it helpful?

Solution

After looking through the Github issues, I found a similar problem and resolved it with the following:

userCtrl.loadUser = ['Restangular', '$route',
    function(Restangular, $route) {
        return Restangular.one('users', $route.current.params.userId).get();
    }];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top