Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

angularjs

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.

like image 947
Timsen Avatar asked Feb 18 '14 23:02

Timsen


People also ask

How do I use $Watch in AngularJS?

$watch(function() {}, function() {} ); The first function is the value function and the second function is the listener function. The value function should return the value which is being watched. AngularJS can then check the value returned against the value the watch function returned the last time.

What is $state in AngularJS?

$stateProvider is used to define different states of one route. You can give the state a name, different controller, different view without having to use a direct href to a route. There are different methods that use the concept of $stateprovider in AngularJS.


2 Answers

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 }) 
like image 115
Gabe Avatar answered Sep 28 '22 09:09

Gabe


This works:

$scope.$watch(function(){     return $state.$current.name }, function(newVal, oldVal){     //do something with values })  
like image 44
Crhistian Ramirez Avatar answered Sep 28 '22 08:09

Crhistian Ramirez