【问题标题】:AngularJS controller doesn't work after first refresh page第一次刷新页面后AngularJS控制器不起作用
【发布时间】:2016-04-09 22:27:34
【问题描述】:

我将 AngularJS 用于我的混合 Ionic 应用程序。

我的控制器:

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

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

有什么想法吗?

【问题讨论】:

标签: javascript angularjs ionic-framework angularjs-controller


【解决方案1】:

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

  <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/

【讨论】:

  • 这只是带有ionic的ui-router的一个特性还是ui-router本身也支持这个功能?
  • @Aides 这也是我的问题。
  • 是的,ui-router支持这个功能
  • 我找到了 angularjs 的这个答案(没有离子)。但是我没有测试过:stackoverflow.com/questions/27690834/…
【解决方案2】:

虽然@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/。希望这会有所帮助,问候!

【讨论】:

  • 谢谢。你的解决方案也很棒。
猜你喜欢
  • 2017-12-30
  • 2011-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-15
  • 1970-01-01
  • 2014-08-23
  • 2020-02-15
相关资源
最近更新 更多