Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...