How to re-run the controller every time you navigate to the corresponding view/state?

跟風遠走 提交于 2019-12-24 11:54:18

问题


I have a view that is linked up to a controller:

<div ng-controller="myController">
  ...
</div>

the controller:

app.controller('myController', ['$scope',
  // Some code here that calls a webservice and updates the scope
]);

When the controller initially runs, it calls a webservice, returns the data and binds it to the scope. It works great. However when I navigate to another view/state using ui-sref or $state.go() and I navigate back to this view, the controller doesn't call the webservice again.

Is there a way to make sure that every time the state lands on this view and controller, the controller is re-run so to speak?


回答1:


Ionic by default caches view and controller execution to improve performance. You can disable it with cache: false:

$stateProvider.state('myState', {
   cache: false,
   url : '/myUrl',
   templateUrl : 'my-template.html'
});

Or you can disable it at URL level using ui-sref-opts How to put reload option in ui-sref markup

See Caching docs.



来源:https://stackoverflow.com/questions/33384826/how-to-re-run-the-controller-every-time-you-navigate-to-the-corresponding-view-s

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