How to $watch state change of $stateProvider in AngularJS?

Angularjs

Angularjs Problem Overview


I know that I can run:

scope.$watch(someItem, function(){})

But I can't figure out a way to watch over change of $state.$current.name in my application.

Angularjs Solutions


Solution 1 - Angularjs

It's in the docs: https://github.com/angular-ui/ui-router/wiki#state-change-events

$rootScope.$on('$stateChangeStart', 
function(event, toState, toParams, fromState, fromParams){ 
    // do something
})

Solution 2 - Angularjs

This works:

$scope.$watch(function(){
    return $state.$current.name
}, function(newVal, oldVal){
    //do something with values
}) 

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTimsenView Question on Stackoverflow
Solution 1 - AngularjsGabeView Answer on Stackoverflow
Solution 2 - AngularjsCrhistian RamirezView Answer on Stackoverflow