$state.go() 不会在单击 Button 时第一次调用控制器

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

我是 Ionic 开发的新手,遇到了一个很奇怪的问题。

我有两个页面主页和保存模板页面,分别使用 HomeCtrl 和 SaveTemplateCtrl。

我正在从我的保存模板页面重定向到主页。

问题是点击保存按钮重定向发生,但主页控制器不是第一次调用。 HomeCtrl被调用的连续次数。

代码如下:

HomeCtrl:

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

保存模板控件:

.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);
   };

 })

建议并帮助我解决这个问题。

提前致谢, :)

如果您的主页是默认页面,当您第一次进入主页时,HomeCtrl将被执行。但是如果您没有禁用主页的缓存,当您从保存模板页面重定向到主页时, HomeCtrl 由于 ionic 视图缓存机制不会再次执行。为了性能,我们应该尽量不要禁用缓存。如果你每次进入都需要执行一些功能,也许你可以把它们放在$ionicView.beforeEnter事件处理程序中:

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

关于离子视图生命周期的更多信息,请参考http://www.gajotres.net/understanding-ionic-view-lifecycle/。希望这会帮助你。问候。