在 AngularJS 和 Angular-UI-Router 中使用 $urlRouterProvider 匹配任何路由

Matching any route using $urlRouterProvider in AngularJS and Angular-UI-Router

我不是 AngularJS 开发人员,但我得到了一个项目。

现在我需要在每次路由更改时调用以下方法。去哪条路线无关紧要。

window.Intercom("update");

现在我找到了下面这段代码。

function config($urlRouterProvider, $stateProvider, $translateProvider, $locationProvider, $httpProvider) {

所以在那个方法中我可以访问 $urlRouterProvider,我在文档中读到这里可以检查匹配的路由,即使使用正则表达式也是如此。

Now I'm wondering, is it possible to see any time the router changes url, Am I looking in the right direction?

如果您使用的是新的 Angular UI 路由器版本 1.xx

您可以像这样使用 运行 块:

angular.module('whatever').run(function($transitions){
    $transitions.onSuccess({}, function(){
        window.Intercom("update");
    });
});

如果您使用的是 Angular UI 路由器的旧版本,您可以像这样订阅 $stateChangeSuccess 事件:

angular.module('whatever').run(function($rootScope){
    $rootScope.$on('$stateChangeSuccess', function(){
        window.Intercom("update");
    });
});