Why does AngularJS with ui-router keep firing the $stateChangeStart event?

后端 未结 6 1607
没有蜡笔的小新
没有蜡笔的小新 2020-12-04 18:29

I\'m trying to block all ui-router state changes until I\'ve authenticated the user:

$rootScope.$on(\'$stateChangeStart\', function (event, next, toParams) {         


        
6条回答
  •  南方客
    南方客 (楼主)
    2020-12-04 18:54

    I also had this issue. Turns out it was the code that they suggested to make a trailing slash optional at https://github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions#how-to-make-a-trailing-slash-optional-for-all-routes

    $urlRouterProvider.rule(function ($injector, $location) {
      var path = $location.url();
    
      console.log(path);
      // check to see if the path already has a slash where it should be
      if (path[path.length - 1] === '/' || path.indexOf('/?') > -1) {
        return;
      }
    
      if (path.indexOf('?') > -1) {
        return path.replace('?', '/?');
      }
    
      return path + '/';
    });
    

    changed this to

    $urlRouterProvider.rule(function ($injector, $location) {
      var path = $location.url();
      // check to see if the path already has a slash where it should be
      if (path[path.length - 1] === '/' || path.indexOf('/?') > -1) {
        return;
      }
      if (path.indexOf('?') > -1) {
        $location.replace().path(path.replace('?', '/?'));
      }
      $location.replace().path(path + '/');
    });
    

    not returning the new path and just replacing it doesn't trigger a StateChangeStart

提交回复
热议问题