1

I'm using AngularJS for my hybrid Ionic app.

My controller:

.controller('SubmitCtrl', function($scope) {
  console.log("this just work for only refreshing the page!);
});

Only for each refreshing the page, console.log works fine, but when I switch to other states, and came back to submit state (without refreshing the page), console.log doesn't work.

Any idea?

6
  • it is bcoz o page cache. Commented Apr 8, 2016 at 14:48
  • @Ved So, what should I do? Commented Apr 8, 2016 at 14:49
  • Could you provide a fiddle? Commented Apr 8, 2016 at 14:49
  • 1
    @vahidnajafi check my answer. Commented Apr 8, 2016 at 14:50
  • 1
    Possible duplicate of controller only initialized once even if `$stateparams` changed Commented Apr 8, 2016 at 14:52

2 Answers 2

4

it is bcoz Ionic cache your page. Try this

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

or,

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

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

Sign up to request clarification or add additional context in comments.

4 Comments

Is this only a feature of ui-router with ionic or does ui-router itself support this as well?
@Aides this is a question for me too.
Yes, this feature is supported by ui-router
I found this answer for angularjs (without ionic). But I haven't tested: stackoverflow.com/questions/27690834/…
1

Though @Ved has answered this question perfectly, disable ionic view cache is not a graceful resolution. If you just want some parts of a page controller to be executed whenever you enter this page, maybe you should make use of ionic view lifecycle:

.controller('SubmitCtrl', function($scope) {
  $scope.$on('$ionicView.beforeEnter', function() {
    //code in this block will be executed every time before you enter in
    console.log("this just work for only refreshing the page!);
  });
});

More information about ionic view lifecycle, please refer http://www.gajotres.net/understanding-ionic-view-lifecycle/ and http://ionicframework.com/docs/api/directive/ionView/. Hope this will help, regards!

1 Comment

Thank you. Your solution is also awesome.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.