$apply already in progress error

后端 未结 11 1252
暗喜
暗喜 2020-11-29 17:53

Stack trace:

Error: $apply already in progress
at Error ()
at beginPhase (file:///android_asset/www/built.min.js:7:22740)
at Object.Scope.$a         


        
相关标签:
11条回答
  • 2020-11-29 18:11

    You can use this statement:

    if ($scope.$root.$$phase != '$apply' && $scope.$root.$$phase != '$digest') {
        $scope.$apply();
    }
    
    0 讨论(0)
  • 2020-11-29 18:15

    Just use $evalAsync instead of $apply.

    0 讨论(0)
  • 2020-11-29 18:16

    We can use setTimeout function in such cases.

    console.log('primary task');
    
    setTimeout(function() {
      
      console.log('secondary task');
    
    }, 0);
    
    

    This will make sure that secondary task will be executed when execution of primary task is finished.

    0 讨论(0)
  • 2020-11-29 18:19

    You are getting this error because you are calling $apply inside an existing digestion cycle.

    The big question is: why are you calling $apply? You shouldn't ever need to call $apply unless you are interfacing from a non-Angular event. The existence of $apply usually means I am doing something wrong (unless, again, the $apply happens from a non-Angular event).

    If $apply really is appropriate here, consider using a "safe apply" approach:

    https://coderwall.com/p/ngisma

    0 讨论(0)
  • 2020-11-29 18:22

    At any point in time, there can be only one $digest or $apply operation in progress. This is to prevent very hard to detect bugs from entering your application. The stack trace of this error allows you to trace the origin of the currently executing $apply or $digest call, which caused the error.

    More info: https://docs.angularjs.org/error/$rootScope/inprog?p0=$apply

    0 讨论(0)
  • 2020-11-29 18:24

    Just resolved this issue. Its documented here.

    I was calling $rootScope.$apply twice in the same flow. All I did is wrapped the content of the service function with a setTimeout(func, 1).

    0 讨论(0)
提交回复
热议问题