Why is using if(!$scope.$$phase) $scope.$apply() an anti-pattern?

后端 未结 6 829
生来不讨喜
生来不讨喜 2020-11-28 02:24

Sometimes I need to use $scope.$apply in my code and sometimes it throws a \"digest already in progress\" error. So I started to find a way around this and foun

6条回答
  •  粉色の甜心
    2020-11-28 02:37

    It is most definitely an anti-pattern now. I've seen a digest blow up even if you check for the $$phase. You're just not supposed to access the internal API denoted by $$ prefixes.

    You should use

     $scope.$evalAsync();
    

    as this is the preferred method in Angular ^1.4 and is specifically exposed as an API for the application layer.

提交回复
热议问题