Wolffyx
Wolffyx

Reputation: 129

Angular $rootScope.on('$stateChangeStart') is not working

I use this to create my website and when i created a run method with $rootScope.on('$stateChangeStart') in it and isn't working.Here is the code:

.run('Url',function($rootScope, $state, $location){
    'ngInject' ;

    console.log('Is working');
    function message(to, toP, from, fromP) {
        return from.name  + angular.toJson(fromP) + " -> " + to.name + angular.toJson(toP);
    }
    $rootScope.$on("$stateChangeStart",   function(evt, to, toP, from, fromP)      {
        console.log("Start:   " + message(to, toP, from, fromP));
        console.log('Not working');
    });

First log when I load the website is working but the second two log are not ,any ideas where the problem is?

Upvotes: 2

Views: 2680

Answers (3)

Oncleroger
Oncleroger

Reputation: 81

If you change of version.

For the new ui-router (Angular 1.x version) . You need to load stateEvents file.

<script src="lib/angular-ui-router/release/stateEvents.min.js"></script>

add load it in your application

angular.module('MyApp', ['ui.router', 'ui.router.state.events', ...

Upvotes: 7

Dujardin Emmanuel
Dujardin Emmanuel

Reputation: 106

Try with this: '$viewContentLoading' or '$viewContentLoaded' :

$rootScope.$on('$viewContentLoading', function() {
    // your stuff here…
});
$rootScope.$on('$viewContentLoaded', function() {
    // your stuff here…
});

Upvotes: 0

Manoj Patidar
Manoj Patidar

Reputation: 1171

Can you use rootScope.$on like as

.run(['$rootScope', '$state', '$location',function($rootScope, $state, $location){  
         console.log('Is working');
         function message(to, toP, from, fromP) {
            return from.name  + angular.toJson(fromP) + " -> " + to.name + angular.toJson(toP);
       }
        $rootScope.$on("$stateChangeStart",   function(evt, to, toP, from, fromP)      {
            console.log("Start:   " + message(to, toP, from, fromP));
            console.log('Not working');
        })
    }]);

Upvotes: 0

Related Questions