AngularJS控制器没有'第一次刷新页面后无法工作

AngularJS controller doesn't work after first refresh page

本文关键字:工作 刷新 第一次 控制器 AngularJS      更新时间:2024-03-08

我正在为我的混合Ionic应用程序使用AngularJS。

我的控制器:

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

仅在每次刷新页面时,console.log工作正常,但当我切换到其他状态并返回到submit状态(不刷新页面)时,console.log不工作。

知道吗?

它是bcoz Ionic缓存您的页面。试试这个

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

或者,

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

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

尽管@Ved完美地回答了这个问题,但禁用离子视图缓存并不是一个合适的解决方案。如果你只想在进入这个页面时执行页面控制器的某些部分,也许你应该利用离子视图生命周期:

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

有关离子视图生命周期的更多信息,请参阅http://www.gajotres.net/understanding-ionic-view-lifecycle/和http://ionicframework.com/docs/api/directive/ionView/.希望这会有所帮助,问候!