问题
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