ui.router 不重新加载控制器 [英] ui.router not reloading controller

查看:23
本文介绍了ui.router 不重新加载控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am developing an application using Ionic Framework and there are multiple views. The route names are app.view1 and app.view2. I transition to the next view form the controller using $state.go("app.view2") and when I click back the controller of app.view1 is not executed again which is very important in my application.

请告诉我如何让控制器在我路由时执行.

Please tell me how to make the controller be executed whenever I route to it.

推荐答案

Ionic 缓存视图以提高性能.它使用了 ng-router 的特性.

Ionic caches views for faster performance. It uses feature of ng-router.

Ionic 最多可以缓存 10 个视图,不仅可以配置,而且应用程序还可以明确说明应该和不应该缓存哪些视图.

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/

有多种方法可以解决此问题.

There are multiple ways to address this issue.

选项 1

如果您希望控制器在每次显示视图时都执行,您可以禁用该特定路由的缓存.

If you want controller to execute everytime the view is shown, you can disable cache for that particular route.

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

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

选项 2

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

You can disable cache for all views. And even control the number of views. I don't know if this could be useful to you or not.

$ionicConfigProvider.views.maxCache(0);

选项 3

我的最爱.您可以使用 $ionicView.enter 事件.这保证了应该执行视图时需要执行的代码.因此,您可以区分代码应该一次执行还是每次执行.我想这会对性能产生积极影响.

My favourite. You can use $ionicView.enter event. This guarantees that the code that needs to be executed when the view is should would be executed. So you can differentiate code should be executed one time and every time. I guess this would have positive impact on performance.

使用

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

祝你好运,编码愉快!

这篇关于ui.router 不重新加载控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆