Mufaddal
Mufaddal

Reputation: 566

$state.go() does not call the controller first time on click of Button

I'm a newbie to Ionic Development, and facing a rather strange problem.

I have two pages Home Page and Save Template Page with HomeCtrl and SaveTemplateCtrl respectively.

From my Save Template Page I'm redirecting to Home page.

The problem is onClick of Save Button redirection happens but the Home Page controller is not called for the First Time. The successive times the HomeCtrl is called.

Code is as below:

HomeCtrl:

.controller('HomeCtrl', function($scope, Template) {
  alert("Within Home Controller");
})

SaveTemplateCtrl :

.controller('SaveTemplateCtrl ', function($scope) {

    $scope.saveTemplate = function(template) {
     //**Logic for Saving the Template
     setTimeout(function() {
       alert("Going to Home Controller");
       $state.go('myapp.home');
    },1200);
   };

 })

Advice and Help me to Solve this issue.

Thanks in Advance, :)

Upvotes: 0

Views: 356

Answers (1)

user6032296
user6032296

Reputation:

If your Home Page is default page, when you go to Home page first time, the HomeCtrl will be executed. But if you have not disabled Home Page's cache, when you redirect to Home Page from Save Template Page, the HomeCtrl would not be excuted again because of ionic view cache mechanism. For performance, we should try best not to disable cache. If you need execute some functions every time you enter in, maybe you could put them in $ionicView.beforeEnter event handler:

    .controller('HomeCtrl', function($scope, Template) {
      $scope.$on('$ionicView.beforeEnter', function(){
        alert("Within Home Controller");
      });
    })

More about ionic view lifecycle, please refer to http://www.gajotres.net/understanding-ionic-view-lifecycle/. Hope this will help you. Regards.

Upvotes: 1

Related Questions