2

I have a controller attached to a state, and everytime said state is accessed, I need my controller to run a block where I do a verification.

How can I do this?

Comic Sans MS Lover
  • 1,729
  • 5
  • 26
  • 52

3 Answers3

2

For reloading controller every time you should mention reload: true option on you .state declaration of ui-router

Sample Code

$stateProvider
.state('state1', {
      templateUrl: 'state1.html',
      controller: `state1Ctrl`,
      reload: true //forcefully reload route and load controller again
})

Also you could refer this SO Question

Community
  • 1
  • 1
Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299
  • Can anyone confirm this works? I don't see it on UI Router docs (http://angular-ui.github.io/ui-router/site/#/api/ui.router.state.$stateProvider) and the reload it's not working for me. – nacho_dh Oct 28 '15 at 22:21
1

In order to call a particular function every time your location changes, you can also define the function in html as:

          <div ng-init="allDealerListing()">

So whenever the particular div in html is loaded the function is called automatically.

Hence upon change of state function is called

Asim
  • 361
  • 1
  • 2
  • 12
0

controller do execute each time you can call an function for example init() function like below

.controller('test',function($scope){
  $scope.init = function( ){
  // your code block here
   }
  $scope.init();
 });
A.B
  • 20,110
  • 3
  • 37
  • 71