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

问题描述

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

据我了解的事情:


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

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

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


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

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

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

感谢您的任何见解。


解决方案

$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().

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.

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 $超时作为一个安全的应用$的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 15:39