Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
225 views
in Technique[技术] by (71.8m points)

javascript - ui.router not reloading controller

I am developing an application using Ionic Framework and there are multiple views. The route names are app.view1 and app.view2. I transition to the next view form the controller using $state.go("app.view2") and when I click back the controller of app.view1 is not executed again which is very important in my application.

Please tell me how to make the controller be executed whenever I route to it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Ionic caches views for faster performance. It uses feature of ng-router.

Ionic will cache a maximum of 10 views, and not only can this be configured, but apps can also explicitly state which views should and should not be cached.

http://ionicframework.com/docs/api/directive/ionNavView/

There are multiple ways to address this issue.

Option 1

If you want controller to execute everytime the view is shown, you can disable cache for that particular route.

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

or

<ion-view cache-view="false" view-title="My Title!">
  ...
</ion-view>

Option 2

You can disable cache for all views. And even control the number of views. I don't know if this could be useful to you or not.

$ionicConfigProvider.views.maxCache(0);

Option 3

My favourite. You can use $ionicView.enter event. This guarantees that the code that needs to be executed when the view is should would be executed. So you can differentiate code should be executed one time and every time. I guess this would have positive impact on performance.

Usage

$scope.$on('$ionicView.enter', function(){
  // Coding
});

Good luck and happy coding!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...