簡體   English   中英

angularjs $ state和$ rootScope。$ on($ stateChangeStart)問題

[英]angularjs $state and $rootScope.$on($stateChangeStart) issue

我經歷了多個問題,但沒有找到解決方案。

我對狀態處理有疑問。

$urlRouterProvider.otherwise( function($injector, $location) {
    var $state = $injector.get("$state");
    $state.go("cover");
});

$stateProvider
    .state('auth', {
        url: '/auth',
        templateUrl: '../views/authView.html',
        controller: 'AuthController as auth'
    })
    .state('users', {
        url: '/users',
        templateUrl: '../views/userView.html',
        controller: 'UserController as user'
    })
....

這就是路由在我的應用中的工作方式。

app.run(function($rootScope, $state, $location) {
    $rootScope.$on('$stateChangeStart', function(event, toState, fromState) {

        //ERROR IS IN THIS BLOCK
        if($rootScope.authenticated && $rootScope.onboard == 0) {

            //event.preventDefault();

            $state.transitionTo("onboard", null, {notify:false});
            $state.go('onboard');
        }
...

每次更改路線時,該阻止都會觸發一百次,但我希望它僅檢查用戶是否已通過身份驗證並填寫了某些表格,或者是否僅重定向到表格本身。

我已經用preventDefault()嘗試了不同的方法; 或沒有,同樣的問題。

首先,我們應該始終嘗試檢查用戶是否尚未轉到(先前重定向)狀態'onboard' 如果是,請停止任何其他處理(只需return )。

$rootScope.$on('$stateChangeStart', function(event, toState, fromState) {

    // in case, that TO state is the one we want to redirect
    // get out of here   
    if(toState.name === "onboard"){
       return;
    }

    if($rootScope.authenticated && $rootScope.onboard == 0) {

        // this should not be commented
        //event.preventDefault();
        // because here we must stop current flow.. .
        event.preventDefault();

        $state.transitionTo("onboard", null, {notify:false});
        $state.go('onboard');
    }
 ...

檢查angularjs ui路由器生成無限循環

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM