如何观察 AngularJS 中的路线变化? [英] How to watch for a route change in AngularJS?

查看:26
本文介绍了如何观察 AngularJS 中的路线变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何观察/触发路线变化的事件?

How would one watch/trigger an event on a route change?

推荐答案

注意:这是对旧版 AngularJS 的正确答案.有关更新版本,请参阅此问题.

Note: This is a proper answer for a legacy version of AngularJS. See this question for updated versions.

$scope.$on('$routeChangeStart', function($event, next, current) { 
   // ... you could trigger something here ...
 });

以下事件也可用(它们的回调函数采用不同的参数):

The following events are also available (their callback functions take different arguments):

  • $routeChangeSuccess
  • $routeChangeError
  • $routeUpdate - 如果 reloadOnSearch 属性有被设置为false
  • $routeChangeSuccess
  • $routeChangeError
  • $routeUpdate - if reloadOnSearch property has been set to false

请参阅 $route 文档.

还有另外两个未记录事件:

  • $locationChangeStart
  • $locationChangeSuccess

参见 $locationChangeSuccess 和 $locationChangeStart 有什么区别?

这篇关于如何观察 AngularJS 中的路线变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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