为什么 $routeChangeSuccess 永远不会被调用? [英] why $routeChangeSuccess never gets called?

查看:41
本文介绍了为什么 $routeChangeSuccess 永远不会被调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的应用程序上做类似于下面的事情,但我只是无法获得 routeChangeSuccess 事件.

var myapp = angular.module('myapp', ["ui.router", "ngRoute"]);myapp.controller("home.RootController",功能($rootScope,$scope,$location,$route){$scope.menus = [{ 'name': 'Home', 'link': '#/home'}, {'name': 'services', 'link': '#/services'} ]$scope.$on('$routeChangeSuccess', function(event, current) {alert('路线改变');});});myapp.config(功能($stateProvider,$urlRouterProvider,$routeProvider){$urlRouterProvider.otherwise("/home");$stateProvider.state('家', {网址:/家",//模板:'

主屏幕

'templateUrl: "/Client/Views/Home/Home.htm"}).state('服务', {网址:/服务",//模板:'<h1>服务界面</h1>'templateUrl: "/Client/Views/Home/service.htm"});});

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

 <ul class="nav"><li ng-repeat="菜单中的菜单" "=""><a href="{{menu.link}}">{{menu.name}}</a>

尚无数据!

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

有什么我遗漏的吗?

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

plnlr 但不能完全正常工作...

问候基兰

解决方案

请查看此 wiki:状态变化事件.摘录:

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

    $scope.$on('$stateChangeSuccess',
    function(event, toState, toParams, fromState, fromParams){ ... })

所以用 $stateChangeSuccess 代替 $routeChangeSuccess.

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

I am doing something similar to below on my app but I am just not able to get the routeChangeSuccess event.

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"

      });
  });

a very simple html as below also fails

  <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>

but when i click on the link i see that the views are getting updated but the $routeChangeSucces event is never triggered.

is there something i am missing?

Another question I had was is there an event that I can hook on to to know the view is ready so I can start some additional processing, like document.ready().

plnlr but not fully working...

Regards Kiran

解决方案

Please, check this wiki: State Change Events. An extract:

  • $stateChangeSuccess - fired once the state transition is complete.

    $scope.$on('$stateChangeSuccess',
    function(event, toState, toParams, fromState, fromParams){ ... })

So instead of the $routeChangeSuccess use the $stateChangeSuccess.

To get more detailed information about all available events, check the wiki Events. Here you can find that the suitable for you, could be event $viewContentLoaded...

这篇关于为什么 $routeChangeSuccess 永远不会被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆