AngularJS UI Router Resolve未被第二次调用

时间:2016-07-12 11:09:08

标签: angularjs angular-ui-router

我正在使用AngularJS UI Router Resolves来检查用户是否已经过身份验证。但它只被召唤一次。第一个它应该工作,但当我再次尝试进入页面时,它会跳过解析部分并打开页面。

.state('login', {
    url: "/login/:state/:stateParams",
    templateUrl: "views/main.html",
    params: {
          stateParams: ""
       }
    })
.state('placeorder', {
    url: "/placeorder",
    templateUrl: "views/placeorder.html",
    params: {
         newOrder: {
             from: null,
             to: null,
             selectedLogType: null,
             selectedLogSubType: null
         }
    },
    resolve: {
        auth: function ($cookies, $stateParams, $location) {
            if (!angular.isDefined($cookies.get('token'))) {
                $location.path('/login/placeorder/'+JSON.stringify($stateParams.newOrder));
            }
            else {
                authService.checkToken().then(function (d) {
                if (d == false) {
                         $location.path('/login/placeorder/'+JSON.stringify($stateParams.newOrder));
                     }
                });
            }
        }
    }
});

更新: 我已经更改了以下内容,并且在没有设置cookie时它可以工作,但是对于异步功能它不起作用。当我登录时,它不起作用($ q.resolve part)

.state('placeorder', {
    url: "/placeorder",
    templateUrl: "views/placeorder.html",
    params: {
        newOrder: {
            from: null,
            to: null,
            selectedLogType: null,
            selectedLogSubType: null
        }
    },
    controller: "placeOrderCtrl",
    resolve: {
        auth: function ($q, $cookies, $timeout, $stateParams, $state) {
            var defer = $q.defer();
            if (!angular.isDefined($cookies.get('token'))) {
                $timeout(function () {
                    $state.go('/login/placeorder/'+JSON.stringify($stateParams.newOrder));
                });
                defer.reject();
            }
            else {
                authService.checkToken().then(function (d) {
                    if (d == false) {
                        $timeout(function () {
                            $state.go('/login/placeorder/'+JSON.stringify($stateParams.newOrder));
                        });
                        defer.reject();
                    }
                    else
                    {
                        defer.resolve();
                    }
                });
            }
            return defer.promise;
        }
    }
})

现在它无法解决。

1 个答案:

答案 0 :(得分:0)

好的,最后我找到了解决方案。无论如何,谢谢:)

fitted(final.arima)