programing

오류: $digest가 이미 진행 중입니다.

mailnote 2023. 4. 1. 09:41
반응형

오류: $digest가 이미 진행 중입니다.

전화를 걸려고 하면 이 에러가 발생합니다.

        function MyCtrl1($scope, $location, $rootScope) {
      $scope.$on('$locationChangeStart', function (event, next, current) {
        event.preventDefault();
        var answer = confirm("Are you sure you want to leave this page?");
        if (answer) {
          $location.url($location.url(next).hash());
          $rootScope.$apply();
        }
      });
    }

MyCtrl1.$inject = ['$scope', '$location', '$rootScope'];

에러는

Error: $digest already in progress

중복:$scope를 호출할 때 이미 진행 중인 오류 $digest를 방지합니다.$syslog()

당신이 받는 오류는 Angular의 더러운 검사가 이미 진행 중이라는 것을 의미합니다.

최근의 베스트 프랙티스는 다음과 같이 말하고 있습니다.$timeout다음 다이제스트 반복에서 코드를 실행하는 경우:

$timeout(function() {
  // the code you want to run in the next digest
});

이전 답변: ( 접근방식은 사용하지 마십시오)

다음과 같이 안전한 적용을 사용합니다.

$rootScope.$$phase || $rootScope.$apply();

상황을 반전시키는 게 어때요?

$scope.$on('$locationChangeStart', function (event, next, current) {                
    if (confirm("Are you sure you want to leave this page?")) {
        event.preventDefault();
    }
});

이 에러의 트러블 슈팅을 검토하고 있는 다른 유저에게 있어서, 문서에서는, 이 에러의 사용을 권장하고 있는 것에 주의해 주세요.$timeout코드가 단일로 호출되도록 하는 서비스$apply차단합니다.

$timeout(function() {
  $scope.someData = someData;
});

또, 이 질문에서는, 인정된 회답의 범위를 넘는 경우도 설명되고 있습니다.

사용하다

$scope.evalAsync(function(){ 
});

대신

$scope.$apply(function(){
});

조스리버의 답변은 내가 겪었던 것과 유사한 $digest 문제를 해결했다.그냥 사용했어요

$scope.$evalAsync(function(){ 
   // code here
});

여기 좋은 기사 https://www.bennadel.com/blog/2605-scope-evalasync-vs-timeout-in-angularjs.htm

왜냐면 당신의 $스코프니까요.$apply()는 AngularJs 환경 내에 있습니다.일반적으로 $rootScope 외에 $apply()를 사용하는 것은 권장하지 않습니다.$syslog() 함수는 응용 프로그램의 실행 속도를 느리게 합니다.전체 범위의 새 사이클이 실행됩니다.

언급URL : https://stackoverflow.com/questions/14838184/error-digest-already-in-progress

반응형