javascript - ui.router 没有重新加载 Controller

标签 javascript angularjs ionic-framework

我正在使用 Ionic Framework 开发一个应用程序,并且有多个 View 。路由名称是 app.view1app.view2。我使用 $state.go("app.view2") 从 Controller 转换到下一个 View ,当我单击返回时,app.view1 的 Controller 不会再次执行,这在我的应用程序中非常重要.

请告诉我如何让 Controller 在我路由到它时执行。

最佳答案

Ionic 缓存 View 以获得更快的性能。它使用了 ng-router 的特性。

Ionic will cache a maximum of 10 views, and not only can this be configured, but apps can also explicitly state which views should and should not be cached.

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

有多种方法可以解决这个问题。

选项 1

如果您希望 Controller 在每次显示 View 时都执行,您可以禁用该特定路由的缓存。

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

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

选项 2

您可以禁用所有 View 的缓存。甚至控制观看次数。我不知道这对你是否有用。

$ionicConfigProvider.views.maxCache(0);

选项 3

我的最爱。您可以使用 $ionicView.enter事件。这保证了应该执行 View 时需要执行的代码。所以你可以区分代码应该执行一次和每次。我想这会对性能产生积极影响。

用法

$scope.$on('$ionicView.enter', function(){
  // Coding
});

祝你好运,编码愉快!

关于javascript - ui.router 没有重新加载 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30991990/

相关文章:

javascript - Leaflet图层,检查feature.properties是否存在

javascript - 为什么 jQuery-UI 可拖动功能不起作用?

javascript - for in Controller 中的循环

angularjs - ionic 应用程序无法使用 $http 连接启用了 cors 的服务器

javascript - 在使用 Angular 计时器满足条件后,用 Promise 取消 $interval

javascript - Datejs - 12 :00 pm 问题

javascript - 如何从Grails外部化属性文件中的Javascript文件中读取属性

javascript - 如何在 ng-repeat 中使用动态 orderBy

javascript - Angular ng-show、ng-hide 和 ng-if 未按预期工作

angular - 如何禁用 'Unused import' - Ionic2/Angular2 上的警告