简体   繁体   English

第一次刷新页面后,AngularJS控制器不起作用

[英]AngularJS controller doesn't work after first refresh page

I'm using AngularJS for my hybrid Ionic app. 我正在为混合离子应用程序使用AngularJS。

My controller: 我的控制器:

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

Only for each refreshing the page, console.log works fine, but when I switch to other states , and came back to submit state (without refreshing the page), console.log doesn't work. 仅对于每次刷新页面, console.log可以正常工作,但是当我切换到其他状态 ,并返回到submit状态(不刷新页面)时, console.log不起作用。

Any idea? 任何想法?

it is bcoz Ionic cache your page. 这是bcoz Ionic缓存您的页面。 Try this 尝试这个

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

or, 要么,

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

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

Though @Ved has answered this question perfectly, disable ionic view cache is not a graceful resolution. 尽管@Ved完美地回答了这个问题,但是禁用离子视图缓存并不是一个合适的解决方案。 If you just want some parts of a page controller to be executed whenever you enter this page, maybe you should make use of ionic view lifecycle: 如果只希望在每次进入此页面时都执行页面控制器的某些部分,则也许应该利用离子视图生命周期:

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

More information about ionic view lifecycle, please refer http://www.gajotres.net/understanding-ionic-view-lifecycle/ and http://ionicframework.com/docs/api/directive/ionView/ . 有关离子视图生命周期的更多信息,请参阅http://www.gajotres.net/understanding-ionic-view-lifecycle/http://ionicframework.com/docs/api/directive/ionView/ Hope this will help, regards! 希望这会有所帮助,问候!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM