0

i am in the porting of some AngularJS services to Angular 8. Some services use $scope. For Example one service use $sScope.$apply in firing events. What can be the better method for porting these service in Angular 8 ?

I can't show any code, but there is some one can point me in the right direction suggesting articles or tutorial for doing this porting.

I have already read simple tutorials, i need some specific examples for doing porting of such things.

Thanks in advance. Giuseppe.

georgeawg
  • 48,608
  • 13
  • 72
  • 95
user783244
  • 111
  • 1
  • 9
  • Read [AngularJS 1.5+ Components do not support Watchers, what is the work around?](https://stackoverflow.com/questions/35534479/angularjs-1-5-components-do-not-support-watchers-what-is-the-work-around). The techniques for avoiding watchers apply to writing components that are upgradeable to Angular 2+ or other frameworks. – georgeawg Feb 08 '20 at 17:37

1 Answers1

1

Angular does the change detection by itself and you do not have to manually trigger the change detection unlike $scope.apply in Angular JS.

So in your case, you could ignore $scope.apply, because that is taken care by Angular

From the docs

The scope.$apply() is how AngularJS detects changes and updates data bindings. After every event that occurs, scope.$apply() gets called. This is done either automatically by the framework, or manually by you.

In Angular things are different. While change detection still occurs after every event, no one needs to call scope.$apply() for that to happen. This is because all Angular code runs inside something called the Angular zone. Angular always knows when the code finishes, so it also knows when it should kick off change detection. The code itself doesn't have to call scope.$apply() or anything like it.

Amit Chigadani
  • 28,482
  • 13
  • 80
  • 98
  • You can still create cases pretty easily where you need to manually run change detection an angular. It generally means you’re doing things wrong, but if you’re porting from angularjs, you probably were doing things the right way then which are now “wrong” – bryan60 Feb 08 '20 at 17:44