运行$ $上的应用rootScope VS任何其他范围 [英] Running $apply on $rootScope vs any other scope
问题描述
的 $适用
功能可以在任何范围内运行,包括 $ rootScope
。
The $apply
function can run on any scope, including $rootScope
.
是否有情况下,当它使,如果我在我的本地范围内运行还是有差别,如果我上运行它,我的 $ rootScope
?
Are there cases when it makes a difference if I run it on my local scope or if I run it on my $rootScope
?
我这么问是因为我想创建一个在 $适用
包装给定功能的辅助函数。要做到这一点我一直需要在一个范围内,这是A)讨厌和B)不容易通过,因为我不一定有本地范围。
I'm asking because I'd like to create a helper function that wraps a given function in an $apply
. To do that I'd always need to pass in a scope, which is A) annoying and B) not easy because I don't necessarily have a local scope.
我想总是有帮助我的函数调用 $适用
在 $ rootScope
,但如果有一个在做一些风险。
I'd like to always have my helper function call $apply
on the $rootScope
, but not if there's some risk in doing that.
推荐答案
运行<code> $适用在任何范围内都会导致一个 $ rootscope。$摘要
。它可能会有所作为的唯一情况是,当你提供一个前pression作为参数传递给 $适用
。这位前pression将在当前范围内(相对于$ rootScope)进行评估,但事后 $ rootscope。$摘要
是总是的所谓
Running $apply
on any scope always results in a $rootscope.$digest
. The only case where it might make a difference is when you provide an expression as an argument to $apply
. The expression will be evaluated in the current scope (vs. $rootScope), but afterwards $rootscope.$digest
is always called.
源$ C $ c是相当清楚的:<一href=\"https://github.com/angular/angular.js/blob/v1.2.16/src/ng/rootScope.js#L886-L901\">rootScope.js
The source code is quite clear: rootScope.js
底线:
如果你调用 $适用
不带参数,它使没有什么区别。
Bottom line:
If you call $apply
with no arguments, it makes no difference.
这篇关于运行$ $上的应用rootScope VS任何其他范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!