How to make so a controller block is executed ever

2020-03-30 03:22发布

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?

3条回答
劫难
2楼-- · 2020-03-30 03:54

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();
 });
查看更多
不美不萌又怎样
3楼-- · 2020-03-30 03:56

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

查看更多
孤傲高冷的网名
4楼-- · 2020-03-30 04:19

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

查看更多
登录 后发表回答