状态更改未触发,但 ui.router 以其他方式工作

State Change not firing but ui.router works otherwise

我正在使用 angularjs:1.6.5 和 ui-路由器:1.0.3

主要html:

<div ng-app="MusicApp" ng-controller="AppCtrl">

    <div ng-include="plugins_url + 'templates/sideMenu.html'"></div>

    <md-content ng-controller="playlistController">

        <div ng-include="plugins_url + 'templates/header.html'"></div>

        <div class="all_content" >
            <div ng-show="!isHome">
                <div ui-view id="other_views" ></div>
            </div>

            <div ng-include="plugins_url + 'templates/main.html'" ng-show="isHome"></div>
        </div>

    </md-content>
[etc]

内容来自sideMenu.html:

<li><a ui-sref="charter">Home</a></li>
<li><a ui-sref="charter.test">Test</a></li>

我的问题是基本上 none 的 stateChange 提供了工作,我试过:$stateChangeError、$stateChangeCancel、$stateChangeStart、$stateChangeSuccess,像这样:

$rootScope.$on('$stateChangeSuccess', function (event, toState, toParams, fromState) {
     console.log('called')
})

但它从未被调用过...试图将其放入 .config 中,在每个控制器中,尝试使用 $scope and/or $rootScope,但是 nada.

奇怪的是,我在控制台中将此视为警告:

angular.min.js?1507208413&ver=4.8.2:123 $route service is not available. Make sure you have included ng-route in your application dependencies.

所以它不知何故无法识别 ui.router,它当然作为文件加载,并作为 'ui.router' 插入到依赖项中。最奇怪的是,ui-sref 正在工作,正如您在 sideMenu.html...

中看到的那样

是否可能存在兼容性问题...某处?

感谢任何帮助!

编辑: 我试过 ui.router 0.4.3 和旧版本,但它甚至无法启动应用程序...

您不能将 $rootScope.$on('$stateChangeSuccess' 用于 ui-router: 1.0.3 (> 1.0.0)

你有 $transitions.on

$transitions.onStart( {}, function (trans) {
  console.log('called');
  console.log(trans.to().name);
  console.log(trans.from().name);
});

参考:https://ui-router.github.io/ng1/docs/latest/modules/ng1_state_events.html#_statechangesuccess

Demo Plunker