javascript Angular 2 - 替代 $scope.$apply?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30734646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angular 2 - Substitute for $scope.$apply?
提问by Jaanus Varus
$scope.$apply
will no longer be part of Angular 2. Then how do we let Angular know to update the DOM if any of the bound properties have been changed outside the regular angular execution context?
$scope.$apply
将不再是 Angular 2 的一部分。那么,如果任何绑定属性在常规 Angular 执行上下文之外发生了更改,我们如何让 Angular 知道更新 DOM?
Taken from a blog post by Minko Gechev:
No more $scope.$apply
But how then AngularJS knows that anything outside it's execution context has taken a place? Lets think where the changes might come from:
- setTimeout
- setInterval
- prompt (yeah, there are people who still use it…)
- XMLHttpRequest
WebSockets
…
没有更多的 $scope.$apply
但是 AngularJS 怎么知道它的执行上下文之外的任何东西都发生了呢?让我们想想变化可能来自哪里:
- 设置超时
- 设置间隔
- 提示(是的,还有人在使用它......)
- XMLHttpRequest
网络套接字
…
For which the answer is:
答案是:
I understand that patching the browser built-in javascript functions to notify of any changes to Angular is something that can be done in a relatively safe manner (without introducing subtle bugs) and would be very convenient for the developer. But what about third party APIs (such as jQuery.fadeIn
) or if the browser exposes some new asynchronous API which isn't covered? What's the substitute for old $scope.$apply
?
我知道修补浏览器内置的 javascript 函数以通知 Angular 的任何更改是可以以相对安全的方式完成的(不会引入细微的错误),并且对开发人员来说非常方便。但是第三方 API(例如jQuery.fadeIn
)或者浏览器是否公开了一些未涵盖的新异步 API 呢?用什么代替旧的$scope.$apply
?
采纳答案by Blaise
So the library that does all this monkey patching is zone.js.
所以完成所有这些猴子补丁的库是zone.js。
jQuery.fadeIn
calls setInterval
, setInterval
is monkey patched, as long as you called jQuery.fadeIn
within a zone.run
.
jQuery.fadeIn
调用setInterval
,setInterval
是猴子修补的,只要您jQuery.fadeIn
在zone.run
.
zone.fork
and zone.run
kind of replace $scope.$apply
, but it's different because it detects itself when asynchronous things have finished, whereas you have to call $scope.$apply
manuallywhen you know things have finished.
See also this question+answer: Use zone.js to detect current execution context from anywhere?
zone.fork
和zone.run
一种 replace $scope.$apply
,但它是不同的,因为它在异步事情完成时检测自己,而当你知道事情已经完成时你必须$scope.$apply
手动调用。另请参阅此问题+答案:使用 zone.js 从任何地方检测当前执行上下文?
if the browser exposes some new asynchronous API which isn't covered?
如果浏览器公开了一些未涵盖的新异步 API?
I guess they will patch that too.
我想他们也会修补它。
If everything else fails, you can still call zone.afterTask()
manually.
I guess that's what you were looking for :)
如果其他一切都失败了,您仍然可以zone.afterTask()
手动调用。
我想这就是你要找的:)
回答by Helzgate
- import
NgZone
from core private zone: NgZone
in your constructorthis.zone.run(() => {});
where you would normallyscope.$apply
NgZone
从核心导入private zone: NgZone
在你的构造函数中this.zone.run(() => {});
你通常会在哪里scope.$apply
or
或者
ChangeDetectorRef
private chRef: ChangeDetectorRef
chRef.detectChanges();
ChangeDetectorRef
private chRef: ChangeDetectorRef
chRef.detectChanges();