Angular 2 - Substitute For $scope.$apply?
$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 ex
Solution 1:
- import
NgZonefrom core private zone: NgZonein your constructorthis.zone.run(() => {});where you would normallyscope.$apply
or
ChangeDetectorRefprivate chRef: ChangeDetectorRefchRef.detectChanges();
Solution 2:
So the library that does all this monkey patching is zone.js.
jQuery.fadeIn calls setInterval, setInterval is monkey patched, as long as you called jQuery.fadeIn within a 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.$applymanually when you know things have finished.
See also this question+answer: Use zone.js to detect current execution context from anywhere?
if the browser exposes some new asynchronous API which isn't covered?
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 :)
Post a Comment for "Angular 2 - Substitute For $scope.$apply?"