state.go()的角度ui-router问题

时间:2015-08-12 11:40:36

标签: angularjs angular-ui-router

我有检查以查看用户可以访问$ stateChangeStart中执行的页面的天气

这是

$rootScope.$on('$stateChangeStart', function (event, toState) {
        if ($rootScope.accessRights.length > 0) {
            if (toState.data.accessControlled == true) {

                var userHasAccess = false;
                for (var i = 0; i < $rootScope.accessControlledPages.length; i++) {
                    if (toState.name == $rootScope.accessControlledPages[i].page) {
                        userHasAccess = true;
                        break;
                    }
                }
                //If user has access do nada, else redirect them to 404 page or page to TBD
                if (!userHasAccess) {
                    event.preventDefault();
                    $state.go('Errors');
                }   
            }
        }
        else
        {
            processPageAccess();
            $state.go(toState.name);
        }

    });

我遇到的问题是     $ state.go(&#39;错误&#39);

什么都没做,我只是停留在我尝试导航到访问控制页面时我所在的页面上。有什么我不做的事吗

3 个答案:

答案 0 :(得分:2)

我知道你需要这样的东西来避免状态变化循环:

$rootScope.$on('$stateChangeStart',function (event, toState, toParams, fromState, fromParams) {
event.preventDefault();
// If authorized, use call state.go without triggering the event.
// Then trigger $stateChangeSuccess manually to resume the rest of the process
// Note: This is a pseudo-hacky fix which should be fixed in future ui-router versions
if (!$rootScope.$broadcast('$stateChangeStart', toState, toParams, fromState, fromParams).defaultPrevented) {
    $rootScope.$broadcast('$stateChangePermissionAccepted', toState, toParams);

    $state.go(toState.name, toParams, {notify: false}).then(function() {
        $rootScope.$broadcast('$stateChangeSuccess', toState, toParams, fromState, fromParams);
   });
}
})

例如,您可以看到here

答案 1 :(得分:0)

if (!userHasAccess) {
   $state.go('Errors');
}

只需删除event.preventDefault();

即可

event.preventDefault();用于取消状态转换。

答案 2 :(得分:0)

白痴错误,没有向控制器注入$ state。最近从常规角度路由转移到ui-router并且没有更新所有控制器