角$范围。$适用VS $超时作为一个安全的应用$ [英] Angular $scope.$apply vs $timeout as a safe $apply

查看:115
本文介绍了角$范围。$适用VS $超时作为一个安全的应用$的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更好地理解角度使用$超时服务作为一种安全$适用法的细微差别。基本上在一块code都可以根据到任何一个角度的事件或作为jQuery的或者一些标准的DOM事件无棱角事件,例如运行场景。

据我了解的事情:


  1. 包装code $中的范围。$申请精品工程的情况下,您
    是不是已经在消化循环(亦称jQ​​uery的事件),但如果一个消化过程中会产生一个错误

  2. 包装code在$超时()调用无延迟参数是否工作已处于消化周期或不

综观角源$ C ​​$ C,它看起来像$超时使得至$ rootScope的调用。$适用()。


  1. 为什么不$超时()也如摘要周期正在进行中引发错误?

  2. 就是用$范围内的最佳实践。$适用()的时候,你肯定知道摘要不会已经在进步和$超时()需要它的时候是安全的无论哪种方式?

  3. 为$超时()真的可以接受的安全应用,还是有陷阱?

感谢您的任何见解。


解决方案

  

综观角源$ 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:

  1. 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
  2. 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().

  1. Why doesn't $timeout() also raise an error if a digest cycle is already in progress?
  2. 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?
  3. 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 via window.setTimeout(fn, delay), which will always run outside of Angular life-cycle. Only once window.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 that FormController 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屋!

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