-1

I'm not an AngularJS dev but I was handed a project.

Now I need to call the following method, every time a route changes. doesn't matter to which route, just any route.

window.Intercom("update");

Now I found the following piece of code.

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

So in that method I have access to the $urlRouterProvider, I read in the docs that here it's possible to check for matched routes, even with regex.

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

georgeawg
  • 48,608
  • 13
  • 72
  • 95
Miguel Stevens
  • 8,631
  • 18
  • 66
  • 125

1 Answers1

3

If you're using the new Angular UI Router version 1.xx

You could just use a run block like so:

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

If you're using the legacy version of Angular UI Router you could just subscribe to $stateChangeSuccess events like so:

angular.module('whatever').run(function($rootScope){
    $rootScope.$on('$stateChangeSuccess', function(){
        window.Intercom("update");
    });
});
JC Ford
  • 6,946
  • 3
  • 25
  • 34
  • Thank you for your answer, I see that $rootscope.$on('$stateChangeStart') is already there.. Is that the place to initialize the plugin? Since I need to initialize it on first run, and then update on the succes thing. Or am I seeing it wrong? – Miguel Stevens Jul 05 '17 at 20:49
  • A `.run()` block executes only once, after the app bootstraps. In this case, it's executing once and adding an event handler. So the `.run()` block runs once, then the callback for the event handler will run every time the state changes. – JC Ford Jul 05 '17 at 20:51
  • Hmh.. almost understanding it, so inside the run() method using the stateChangeStart to init the plugin, and the stateChangeSuccess to update it on every new route isn't going to work? – Miguel Stevens Jul 05 '17 at 20:54
  • What are you using `$stateChangeStart` for? That event will fire before every potential state change. State changes might be prevented, though. `$stateChangeSuccess` only fires if the state actually changes. – JC Ford Jul 05 '17 at 20:55
  • `$stateChangeStart` is usually used to check authentication or access rights and to reject the state change or redirect the user if necessary. – JC Ford Jul 05 '17 at 20:58
  • Well I'm trying to integration Intercom, It's a plugin and they ask "Once he app initializes do the following script", then "Do the other script every time the view and/or URL changes in your app.".. Thanks for your time! – Miguel Stevens Jul 05 '17 at 20:59
  • 1
    Right, so the "do once the app initializes" code can be at the top of the `.run()` block and the "do every time the view changes" code should go in the `$stateChangeSuccess` handler. – JC Ford Jul 05 '17 at 21:01
  • Thank you so much! I'll try and test this. and i'll accept your answer offcourse – Miguel Stevens Jul 05 '17 at 21:03