javascript $ionicView.enter 和 cache:false 有什么区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31073765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between $ionicView.enter and cache:false
提问by Felipe
I'm developing a view that need call multiples methods of a webservice every time the view is opened, should i use $scope.$on('$ionicView.enter', function(){...})
or cache:false
?
我正在开发一个视图,每次打开视图时都需要调用 Web 服务的多个方法,我应该使用$scope.$on('$ionicView.enter', function(){...})
还是cache:false
?
What is the real difference between each one?
每个人之间的真正区别是什么?
回答by Radim K?hler
I really enjoyed this Q & A:
我真的很喜欢这个问答:
ui.router not reloading controller
ui.router 不重新加载控制器
Where the Bipin Bhandarinicely summarizes the options we have with ionic caching mechanism
凡比拼班达里很好地总结了选择,我们有离子缓存机制
- avoid caching by
cache: false
, - disable caching with
$ionicConfigProvider.views.maxCache(0)
; - or keep caching as is, and let controller be executed only once ... while doing some smart stuff during these View LifeCycle and Events
- 避免缓存
cache: false
, - 禁用缓存
$ionicConfigProvider.views.maxCache(0)
; - 或者保持缓存不变,让控制器只执行一次......同时在这些View LifeCycle 和 Events期间做一些聪明的事情
So, with caching in place, controller will be executed just once:
因此,在缓存到位后,控制器将只执行一次:
Views are cached to improve performance. When a view is navigated away from, its element is left in the DOM, and its scope is disconnected from the $watch cycle. When navigating to a view that is already cached, its scope is reconnected, and the existing element, which was left in the DOM, becomes active again.
缓存视图以提高性能。当一个视图被导航离开时,它的元素留在 DOM 中,它的作用域与 $watch 循环断开连接。当导航到已经缓存的视图时,它的作用域会重新连接,并且留在 DOM 中的现有元素再次变为活动状态。
We can hook on these events... to do some "always stuff" with this controller
我们可以钩住这些事件……用这个控制器做一些“永远的事情”
回答by aorfevre
$ionicView.enter
is an event that is broadcasted each time the selected view is activated.
$ionicView.enter
是每次激活所选视图时广播的事件。
cache:false
means that the page will never be cached, and is therefore, reloaded completely each time.
cache:false
意味着页面永远不会被缓存,因此每次都完全重新加载。
I personnaly try to avoid using cache false as it as bad performances but has side effects as your controller won't be initialised again when you are back on it.
我个人尽量避免使用缓存 false ,因为它会导致性能不佳,但会产生副作用,因为当您重新使用时,您的控制器不会再次初始化。
Instead, when I enter a view, I user $ionicView.enter
or $ionicView.afterEnter
to trigger several actions for page to completly finished the loading.
相反,当我进入一个视图时,我使用$ionicView.enter
或$ionicView.afterEnter
触发页面的几个动作来完全完成加载。