AngularJS服务rootscopeAngularJS、rootscope

2023-09-13 03:45:13 作者:不适合动情的年纪 ˉ

我在AngularJS定义的 app.factory(服务名功能($ rootScope){..})服务如果我做 $ rootScope。$适用(),将是重新评估在应用中的所有控制器的范围或使用本服务的控制器只是范围?

I have a service defined in AngularJS with app.factory("serviceName", function($rootScope) { .. }) If I do $rootScope.$apply(), would that re-evaluate ALL the controller's scopes in the app or just the scopes of the controllers which use this service?

推荐答案

$消化()唯一的刷新当前范围及其所有子作用域。

$digest() only "flushes" the current scope and all of its child scopes.

$申请(EXP)评估记录,然后调用$消化()在根范围内。所以调用$适用()(在任何范围)影响的所有范围。

$apply(exp) evaluates the exp and then calls $digest() on the root scope. So calling $apply() (on any scope) affects all scopes.

如果你调用$消化()和你的行动改变一些父范围,改变不会被观察到。所以通常你要调用$适用()。 (如果你很想尝试,并通过调用$摘要(),它可以在以后咬你更有效率!)

If you call $digest() and your action changes some parent scope, the change will not be observed. So normally you want to call $apply(). (If you're tempted to try and be more efficient by calling $digest(), it can bite you later!)