router不重装控制器

router不重装控制器

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

问题描述

我正在开发使用离子型框架的应用程序,并有多种看法。路由名称的 app.view1 app.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.

推荐答案

离子缓存的意见更快的性能。它使用的功能 NG-路由器

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

离子将最多10次缓存,不仅可以在此配置,但应用程序也可以明确指出哪些意见应该和不应该被缓存。

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 事件。这保证了当应该将被执行的视图要执行的code需要。所以,你可以区分code应一次,每一次执行。我想这会对性能产生积极的影响。

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

祝你好运和快乐编码!

Good luck and happy coding!

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