【问题标题】:why $routeChangeSuccess never gets called?为什么 $routeChangeSuccess 永远不会被调用?
【发布时间】:2018-02-27 21:49:30
【问题描述】:

我正在我的应用程序上执行类似于下面的操作,但我无法获得 routeChangeSuccess 事件。

var myapp = angular.module('myapp', ["ui.router", "ngRoute"]);

myapp.controller("home.RootController",
  function($rootScope, $scope, $location, $route) {
    $scope.menus = [{ 'name': 'Home ', 'link': '#/home'}, {'name': 'services', 'link': '#/services'} ]

    $scope.$on('$routeChangeSuccess', function(event, current) {

      alert('route changed');
    });

  }
);

myapp.config(
  function($stateProvider, $urlRouterProvider, $routeProvider) {
    $urlRouterProvider.otherwise("/home");
    $stateProvider
      .state('home', {
        url: "/home",
        //template: '<h1>Home Screen</h1>'
        templateUrl: "/Client/Views/Home/Home.htm"
      })
      .state('services', {
        url: "/services",
        //template: '<h1>Service screen</h1>'
        templateUrl: "/Client/Views/Home/service.htm"

      });
  });

一个非常简单的html如下也失败了

  <body ng-controller="home.RootController">

    <ul class="nav">
      <li ng-repeat="menu in menus" "="">
        <a href="{{menu.link}}">{{menu.name}}</a>
      </li>
    </ul>
    <div ui-view> No data yet!</div>
  </body>

但是当我点击链接时,我看到视图正在更新,但从未触发 $routeChangeSucces 事件。

我有什么遗漏吗?

另一个问题我有一个事件,我可以知道视图已准备好,这样我就可以开始一些额外的处理,比如 document.ready()。

plnlr 但未完全正常工作...

问候 基兰

【问题讨论】:

  • ui.router 不能与ngRoute 一起使用。请使用其中一种。

标签: angularjs angular-ui angular-ui-router


【解决方案1】:

请查看此维基:State Change Events。摘录:

  • $stateChangeSuccess - 状态转换完成后触发。

    $scope.$on('$stateChangeSuccess',
    函数(事件,toState,toParams,fromState,fromParams){ ... })

所以不要使用$routeChangeSuccess,而是使用$stateChangeSuccess

要获取有关所有可用事件的更多详细信息,请查看 wiki Events在这里你可以找到适合你的,可能是事件$viewContentLoaded...

【讨论】:

  • 谢谢! $viewContentLoaded 是我正在寻找的。顺便说一句,我无法获得 $viewContentLoading 事件。不知道为什么。但 $viewContentLoaded 工作得很好
  • 类似地,查看$stateChangeStart
【解决方案2】:

stateChange 事件现在已被弃用并删除,请改用transitions

$transitions.onSuccess({}, function () {
    console.log("state changed");
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-22
    • 2018-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-16
    • 2014-08-23
    相关资源
    最近更新 更多