AngularJS 控制器在第一次刷新页面后不工作

AngularJS controller doesn't work after first refresh page

我正在为我的混合 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/ and http://ionicframework.com/docs/api/directive/ionView/。希望这会有所帮助,问候!