首页 > 解决方案 > ui-router (0.3.2) 不会为历史记录上的正则表达式状态参数触发`$stateChangeStart`

问题描述

请参阅此堆栈闪电战。请注意,我正在使用Angularjs@1.5.7and ui-router@0.3.2

它定义了以下 ui-router 状态:

  $stateProvider
  .state('counter1', {
    url: '/counter1/{id:(NEW-ID-\d+|)}?detail',
    templateUrl: 'view/counter.html',
    controller: ['$scope', '$state', '$interval', counterController]
  })
  .state('counter2', {
    url: '/counter2/:id?detail',
    templateUrl: 'view/counter.html',
    controller: ['$scope', '$state', '$interval', counterController]
  })

两种状态是相同的,只是counter1将参数定义id为正则表达式,而counter2以简单的方式定义它。

问题是,通过这个counterController设置

$scope.$on('$stateChangeStart', () => console.log('$stateChangeStart'))

只会为counter2路由触发,不会counter1在用户使用浏览器的后退前进功能时触发。奇怪的是,它确实会触发例如state.go()或单击ui-sref链接。

如果counter1路由中的正则表达式更改为{id:.*}它确实可以按预期工作,但对于我的用例,我需要将其定义为{id:NEW-ID-\d+|}. 这是一个已知的错误吗?是否有任何其他解决方法可以确保$stateChangeStart实际触发正确?我还没有找到任何东西,我会很感激你的想法。

标签: javascriptangularjsangular-ui-router

解决方案


推荐阅读