角$范围。$适用VS $超时作为一个安全的应用$ [英] Angular $scope.$apply vs $timeout as a safe $apply
问题描述
我想更好地理解角度使用$超时服务作为一种安全$适用法的细微差别。基本上在一块code都可以根据到任何一个角度的事件或作为jQuery的或者一些标准的DOM事件无棱角事件,例如运行场景。
据我了解的事情:
- 包装code $中的范围。$申请精品工程的情况下,您
是不是已经在消化循环(亦称jQuery的事件),但如果一个消化过程中会产生一个错误 - 包装code在$超时()调用无延迟参数是否工作已处于消化周期或不
综观角源$ C $ C,它看起来像$超时使得至$ rootScope的调用。$适用()。
- 为什么不$超时()也如摘要周期正在进行中引发错误?
- 就是用$范围内的最佳实践。$适用()的时候,你肯定知道摘要不会已经在进步和$超时()需要它的时候是安全的无论哪种方式?
- 为$超时()真的可以接受的安全应用,还是有陷阱?
感谢您的任何见解。
综观角源$ C $ C,它看起来像$超时使一个呼叫
$ rootScope。$适用()。
- 为什么不$超时()也如摘要周期正在进行中引发错误?
块引用>
$超时
利用无证角服务$浏览器
。具体来说,它使用$ browser.defer()
异步通过window.setTimeout(FN,延迟)
,这将永远运行的角度生命周期之外。只有当window.setTimeout
已触发你的函数将$超时
通话$ rootScope。$申请()
。
- 就是用$范围内的最佳实践。$适用()的时候,你肯定知道摘要不会已经在进步和$超时()需要它的时候是安全的无论哪种方式?
块引用>我会这么说。另一个用例是,有时你需要访问你只知道将消化后初始化$范围的变量。如果你想设置你的控制器构造(无论何种原因)内的表单的状态脏简单的例子是。如果没有$超时
的FormController
尚未初始化并公布到$范围,所以包装$ scope.yourform.setDirty()
在$超时确保的FormController
已初始化。当然,你可以做到这一切有没有指令$超时,只是给另一使用情况实例。
- 为$超时()真的可以接受的安全应用,还是有陷阱?
块引用>它应该永远是安全的,但你转到方法应始终瞄准$适用()在我看来。当前角度应用我的工作是相当大,我们只有依靠$超时一次,而不是$适用()。
I'm trying to better understand the nuances of using the $timeout service in Angular as a sort of "safe $apply" method. Basically in scenarios where a piece of code could run in response to either an Angular event or a non-angular event such as jQuery or some standard DOM event.
As I understand things:
- Wrapping code in $scope.$apply works fine for scenarios where you aren't already in a digest loop (aka. jQuery event) but will raise an error if a digest is in progress
- Wrapping code in a $timeout() call with no delay parameter works whether already in a digest cycle or not
Looking at Angular source code, it looks like $timeout makes a call to $rootScope.$apply().
- Why doesn't $timeout() also raise an error if a digest cycle is already in progress?
- Is the best practice to use $scope.$apply() when you know for sure that a digest won't already be in progress and $timeout() when needing it to be safe either way?
- Is $timeout() really an acceptable "safe apply", or are there gotchas?
Thanks for any insight.
解决方案Looking at Angular source code, it looks like $timeout makes a call to $rootScope.$apply().
- Why doesn't $timeout() also raise an error if a digest cycle is already in progress?
$timeout
makes use of an undocumented Angular service$browser
. Specifically it uses$browser.defer()
that defers execution of your function asynchronously viawindow.setTimeout(fn, delay)
, which will always run outside of Angular life-cycle. Only oncewindow.setTimeout
has fired your function will$timeout
call$rootScope.$apply()
.
- Is the best practice to use $scope.$apply() when you know for sure that a digest won't already be in progress and $timeout() when needing it to be safe either way?
I would say so. Another use case is that sometimes you need to access a $scope variable that you know will only be initialized after digest. Simple example would be if you want to set a form's state to dirty inside your controller constructor (for whatever reason). Without $timeout the
FormController
has not been initialized and published onto $scope, so wrapping$scope.yourform.setDirty()
inside $timeout ensures thatFormController
has been initialized. Sure you can do all this with a directive without $timeout, just giving another use case example.
- Is $timeout() really an acceptable "safe apply", or are there gotchas?
It should always be safe, but your go to method should always aim for $apply() in my opinion. The current Angular app I'm working on is fairly large and we've only had to rely on $timeout once instead of $apply().
这篇关于角$范围。$适用VS $超时作为一个安全的应用$的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!