$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?
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
…
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
?
The controllers and $scope in Angular 1 have been replaced with “Components” in Angular 2. Hence we can say that it is a component-based framework, which uses zone.
Angular 2 is component based. Components combine concepts that we are already familiar with from AngularJS. The Angular 2 Component combines the AngularJS Directive, Controller, and Scope.
In Angular 2, controllers and $scope were replaced by components and directives. Components are directives with a template.
What is alternative of rootScope in Angular? In similar way, you can use use the sharedService inside any component & fetch the returing output and used inside your view. That's it. I found this solution as a good alternative of rootScope.
NgZone
from coreprivate zone: NgZone
in your constructorthis.zone.run(() => {});
where you would normally scope.$apply
or
ChangeDetectorRef
private chRef: ChangeDetectorRef
chRef.detectChanges();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With