Angular route决定不等待承诺

时间:2014-03-17 01:17:26

标签: javascript angularjs angularjs-routing

我是角色新手,我有一条用户路线,我试图在渲染视图之前解析用户对象。我已注入$ q并推迟了承诺,但是,在承诺返回之前,视图仍在加载。

路线:

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

控制器

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;
    }];

1 个答案:

答案 0 :(得分:2)

在查看Github问题后,我发现了类似的问题并通过以下方法解决了这个问题:

userCtrl.loadUser = ['Restangular', '$route',
    function(Restangular, $route) {
        return Restangular.one('users', $route.current.params.userId).get();
    }];