首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用$state.go调用角中的每个$state.go()之前的函数

如何使用$state.go调用角中的每个$state.go()之前的函数
EN

Stack Overflow用户
提问于 2016-07-20 07:35:52
回答 3查看 1.2K关注 0票数 2

标题说明了一切,但更多细节;我想通过在非常$state.go()调用之前检查cookie是否可用,来检查用户是否经过身份验证,是否有可能在全局设置cookie,而不强制在每个state.go()函数上这样做?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-07-20 07:41:35

因此,您可以连接到$stateChangeStart事件,每次转换即将开始时都会发生。文档

你会做这样的事:

代码语言:javascript
复制
app.run(['$state', '$rootScope', function ($state, $rootScope) {
    $rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
        // calling event.preventDefault() will prevent the transition, so this is used
        // when you want to stop certain transitions.
    }
});

现在有两个选项可以更改是否要阻止路由。

您可以使用toStatefromState在路由字典中查找。

无论是否可以,都可以使用servicefactory进行更改。

所以如果你创建一个工厂,--这是一个例子,它给出了的概念

代码语言:javascript
复制
app.factory('routingLogic', function () {
   var singletonShouldRoute = true;
   var service = {
       canRoute: canRoute,
       setCanRoute: setCanRoute
   };
   return service;

   function setCanRoute(shouldRoute) {
      singletonShouldRoute = shouldRoute
   }

   function canRoute() {
      return singletonShouldRoute ;
   }
}

app.controller('somePage', ['routingLogic', '$state', function (routingLogic, $state) {
    var vm = this;

    vm.shouldRoute = false;

    vm.changeShouldRoute = function () {
        routingLogic.setCanRoute(vm.shouldRoute);
    } 

    vm.goNextPage = function () {
        $state.go('some page');
    }
});

app.run(['$state', '$rootScope', 'routingLogic', function ($state, $rootScope, routingLogic) {
    $rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
        // calling event.preventDefault() will prevent the transition, so this is used
        // when you want to stop certain transitions.

        if (!routingLogic.canRoute()) { event.preventDefault(); }
    }
});

但是,您可以使用字典切换出canRoute

代码语言:javascript
复制
app.factory('routingLogic', function () {
   var allowedRoutes = {
      "thisroute": true,
      "thatroute": true
   }

   var service = {
       canRoute: canRoute,
       setCanRoute: setCanRoute
   };
   return service;

   // you can pull this out to provider level if you want it in the config stage
   function setCanRoute(routeName) {
       allowedRoutes[routeName] = true;
   }

   function canRoute(routeName) {
      return allowedRoutes[routeName] || false;
   }
}

app.run(['$state', '$rootScope', 'routingLogic', function ($state, $rootScope, routingLogic) {
    $rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
        // calling event.preventDefault() will prevent the transition, so this is used
        // when you want to stop certain transitions.

        if (!routingLogic.canRoute(toState)) { event.preventDefault(); }
    }
});

如果你只想让特定的路径被选择,你可以更进一步。所以您可以从WizardStep1过渡到WizardStep2,但不能转换到WizardStep3

代码语言:javascript
复制
app.factory('routingLogic', function () {
   var allowedRoutes = {
      "thisroute": ["thatroute", "theOtherRoute"],
      "thatroute": ["home"]
   }

   var service = {
       canRoute: canRoute,
       setCanRoute: setCanRoute
   };
   return service;

   // you can pull this out to provider level if you want it in the config stage
   function setCanRoute(routeName) {
       allowedRoutes[routeName] = true;
   }

   function canRoute(fromRoute, toRoute) {
      var allowedTo = allowedRoutes[fromRoute] || [];

      return allowedTo.some(function (allowedRoute) { return allowedRoute === toRoute; });
   }
}
票数 1
EN

Stack Overflow用户

发布于 2016-07-20 07:39:09

你可以使用“$stateChangeStart”

代码语言:javascript
复制
app.run(function($rootScope, $state, Auth) {
    $rootScope.$on('$stateChangeStart', function(event, newUrl, oldUrl) {
        if (!Auth.isLoggedIn()) {
            event.preventDefault();
            $state.go('login', {
                next: newUrl.name
            });
        }
    });
})

也可以使用http拦截器进行API调用。

代码语言:javascript
复制
.factory('authInterceptor', ['$q', '$cookieStore', '$location', function($q, $cookieStore, $location) {
    return {
        // Intercept 401s and redirect you to login
        responseError: function(response) {
            if (response.status === 401) {
                $location.path('/login');
                // remove any stale tokens
                $cookieStore.remove('token');
            }
        }
    };
}])
票数 1
EN

Stack Overflow用户

发布于 2016-07-20 07:42:16

假设,您有一个主模块

代码语言:javascript
复制
angular
    .module('app.core')
    .run(appRun);

可以将状态更改的appRun函数处理程序添加到

代码语言:javascript
复制
$rootScope.$on('$stateChangeStart', function(
        evt, toState, toParams, fromState, fromParams
    ) {
        ...Do your stuff here...
    });
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38475095

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档