Does it always use the value of $ scope. $ Apply?

Sometimes I need to use $scope.$apply, for example, when I use jQuery ajax or some non-w500> -js eventlisteners. In such cases, I have an asynchronous callback, and there I use $scope.$applyso that changes in the area are picked up by angular and the clock works. Please correct me if this is a misuse $scope.$apply.

This seems to work and the view is being updated. However, in some fairly rare cases, I get a "digest is already in process" error. I do not see how this is possible, since the callback is not synchronous. So I'm wondering if it is possible that my asynchronous callback with $scope.$applyin it might accidentally run into the current digest? If so, how can I prevent this?

change

One way to check the digest is to check $$ phase:, if (!$scope.$$phase) $scope.$apply()but this is an anti-pattern, as the wiki says: https://github.com/angular/angular.js/wiki/Anti-Patterns

I want to fully understand why it is possible that I came across a digest in an asynchronous callback. And why is it antipattern?

+4
source share
4

, if (!$scope.$$phase) $scope.$apply() - : if (! $scope. $$ phase) $scope. $apply ( ) -?

.

+1

$apply , , , . $$ phase, -

, , . , - , $scope.$apply .

, $apply $- . throttle $apply, . - socket.io, , .

+1

, , angular. ,

scope.$$phase

$apply

0

Asynchronous callbacks are the right time to use $ apply, there are edgecases that I came across and where I get this error. They are usually because I am doing something shaky, I found that "Conditional Application" works well for them.

if(!$scope.$$phase) {
  //$digest or $apply
}

AngularJS: $ digest error prevention already occurs when calling $ scope. $ apply ()

0
source

Source: https://habr.com/ru/post/1531198/


All Articles