How to make so a controller block is executed everytime a state is accessed?

别说谁变了你拦得住时间么 提交于 2020-01-11 06:42:11

问题


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?


回答1:


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




回答2:


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




回答3:


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();
 });


来源:https://stackoverflow.com/questions/29355637/how-to-make-so-a-controller-block-is-executed-everytime-a-state-is-accessed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!