1

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;
    }];
2
  • It might be that the promise is not being resolved successfully. As per the docs, $routeProvider should wait until all promises are resolved before firing the $routeChangeSuccess event and switching the view. Commented Mar 17, 2014 at 3:28
  • Please put your answer in an... answer and accept it :) Commented Mar 17, 2014 at 21:32

1 Answer 1

2

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();
    }];
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.