javascript - 如何使用 stateProvider 在 Angular 中的每个 $state.go() 之前调用函数

标签 javascript jquery angularjs

标题说明了一切,但还有更多细节;我想通过在 $state.go() 调用之前检查 cookie 是否可用来检查用户是否已通过身份验证,是否可以全局设置它,而不是被迫在每个state.go() 函数?

最佳答案

因此,您可以 Hook 每次转换即将开始时发生的 $stateChangeStart 事件。 Docs

你会做这样的事情:

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在路由字典中查找。

您可以使用服务工厂来更改是否可以。

因此,如果您创建一个工厂,这是一个示例,它为您提供了概念:

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:

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:

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

关于javascript - 如何使用 stateProvider 在 Angular 中的每个 $state.go() 之前调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38475095/

相关文章:

javascript - 为什么我的循环不在我的页面上显示我的 JSON/API 数据?

javascript - jQuery查询字符串遍历

angularjs - Angular JS View 无法正确更新

javascript - Angular 路由入门

javascript - 我有一个从起草者那里获得的 json 对象,我只想要模式

javascript - 如何通过 javascript 更新 Bootstrap 选择 (selectpicker) header

javascript - jQuery - 遍历样式

javascript - angularjs的动态函数调用

javascript - 如果我使用 babel-polyfill,我可以使用 Typescript 来定位 ES6 吗?

javascript - 使用 JavaScript 验证货币