【问题标题】:State Change not firing but ui.router works otherwise状态更改未触发,但 ui.router 以其他方式工作
【发布时间】:2018-03-17 03:16:03
【问题描述】:

我正在使用 angularjs:1.6.5 和 ui-router: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>

我的问题是 stateChange 基本上没有一个提供工作,我尝试过:$stateChangeError、$stateChangeCancel、$stateChangeStart、$stateChangeSuccess,如下所示:

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

但它从未被调用...尝试将它放在 .config 中,在每个控制器中,用 $scope 和/或 $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 及旧版本,但它甚至无法启动应用程序...

【问题讨论】:

    标签: angularjs angular-ui-router


    【解决方案1】:

    您不能将$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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-03
      • 2020-11-06
      • 2018-08-19
      • 2019-07-04
      • 2018-09-12
      • 2022-01-19
      • 2014-08-26
      • 1970-01-01
      相关资源
      最近更新 更多