对角位置的变化位置

2023-09-13 04:43:47 作者:他走了她哭了我笑了

有没有一种方法来检测AngularJS全局位置的变化,而不仅仅是单个控制器?我的目标是检测每个位置的变化。还是有看window.location.href更改任何有效的方式?

Is there a way to detect global location changes in AngularJS, not just single controller? My goal is to detect every location change. Or is there any effective way to watch window.location.href for changes?

$routeChangeSuccess 

我的理解是唯一的单控制器或我错了?

As I understood is for single controller only or I'm wrong?

推荐答案

首先, $ routeChangeSuccess 不局限于单一的控制器。它是从广播 $ rootScope 这意味着它可以听取了每一个范围(或每一个从 $ rootScope ),但是是一个应用广泛的事件。

First of all, $routeChangeSuccess is not limited to a single controller. It is broadcast from the $rootScope which means that it can be listened to on every scope (or, every scope that inherits from $rootScope) but is an application wide event.

此外,还有工作方式类似于 $ routeChangeSuccess 称为无证事件 $ locationChangeSuccess 。所不同的是,前者火灾一旦某条路已经成功改变了后者的火灾时,URL被改变,但该路由的变化。请注意,这是不是所有的URL发生变化,只是时间的方式,在AngularJS应用程序可以注册的URL更改(例如,一个二传手调用 $ location.url())。

There is also an undocumented event that works similar to $routeChangeSuccess called $locationChangeSuccess. The difference is that the former fires once a route has successfully changed and the latter fires when the URL is changed but before the route changes. Note that this isn't all URL changes, just times the URL changes in a way that the AngularJS application can register it (for example, a setter call to $location.url()).

只是为了澄清, $ locationChangeSuccess 从广播 $ rootScope 以及

Just to clarify, $locationChangeSuccess is broadcast from the $rootScope as well.

有关这两种,你可以使用范围监听事件。$在('$ routeChangeSuccess')范围。$在('$ locationChangeSuccess')

For both, you can listen to the event using scope.$on('$routeChangeSuccess') or scope.$on('$locationChangeSuccess').