Programing

$ 적용 진행 중 오류

crosscheck 2020. 7. 2. 07:54
반응형

$ 적용 진행 중 오류


스택 추적 :

Error: $apply already in progress
at Error (<anonymous>)
at beginPhase (file:///android_asset/www/built.min.js:7:22740)
at Object.Scope.$apply (file:///android_asset/www/built.min.js:7:25967)
at navigator.geolocation.getCurrentPosition.that (file:///android_asset/www/built.min.js:13:8670)
at Object.geolocation.getCurrentPosition (file:///android_asset/www/plugins/org.apache.cordova.core.geolocation/www/geolocation.js:122:13)
at Object.getCurrentPosition (file:///android_asset/www/built.min.js:13:8589)
at Object.getCurrentPosition (file:///android_asset/www/built.min.js:13:8277)
at Object.getCurrentCity (file:///android_asset/www/built.min.js:13:8941)
at Object.$scope.locateDevice (file:///android_asset/www/built.min.js:13:10480)
at file:///android_asset/www/built.min.js:7:12292:7

이 코드를 참조 http://pastebin.com/B9V6yvFu

    getCurrentPosition: cordovaReady(function (onSuccess, onError, options) {

        navigator.geolocation.getCurrentPosition(function () {
            var that = this,
                args = arguments;

            if (onSuccess) {
                $rootScope.$apply(function () {
                    onSuccess.apply(that, args);
                });
            }
        }, function () {
            var that = this,
                args = arguments;
            if (onError) {
                $rootScope.$apply(function () {
                    onError.apply(that, args);
                });
            }
        }, {
            enableHighAccuracy: true,
            timeout: 20000,
            maximumAge: 18000000
        });
    })

이상한 것은 내 LG4X에서는 잘 작동하지만 삼성 s2에서는 위의 오류가 발생합니다. 어떤 아이디어가 잘못 되었습니까?


$apply기존 소화주기 내에서 호출하기 때문에이 오류가 발생 합니다.

가장 큰 질문은 : 왜 전화하는 $apply거야? $apply비 앵글 이벤트와 인터페이스하지 않는 한 전화를 걸 필요가 없습니다 . $apply일반적으로 존재한다는 것은 내가 잘못한 것을 의미합니다 (다시 말하지만 $ 적용이 비 Angular 이벤트에서 발생하지 않는 한).

$apply여기에 실제로 적절한 경우 "안전한 적용"접근 방식을 사용해보십시오.

https://coderwall.com/p/ngisma


그냥 사용 $ evalAsync을 대신 $apply.


이 문장을 사용할 수 있습니다 :

if ($scope.$root.$$phase != '$apply' && $scope.$root.$$phase != '$digest') {
    $scope.$apply();
}

경우에 따라 범위를 적용해야하는 경우 다음 적용 시간까지 $ apply가 지연되도록 시간 초과를 설정할 수 있습니다.

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

또는 코드를 $ timeout (function () {..}); 실행이 끝날 때 자동으로 범위를 적용합니다. 함수가 동기식으로 작동 해야하는 경우 먼저 할 것입니다.


각도 1.3에서는 새로운 기능-을 추가했다고 생각 $scope.$applyAsync()합니다. 이 함수 호출은 나중에 적용됩니다-그들은 적어도 약 10ms 후에 말합니다. 완벽하지는 않지만 적어도 성가신 오류를 제거합니다.

https://docs.angularjs.org/api/ng/type/ $ rootScope.Scope # $ applyAsync


제 경우 $apply에는 각 캘린더 UI와 함께 사용 하여 일부 이벤트를 연결합니다.

$scope.eventClick = function(event){           
    $scope.$apply( function() {
        $location.path('/event/' + event.id);
    });
};

문제의 문서를 읽은 후 : https://docs.angularjs.org/error/ $ rootScope / inprog

The part Inconsistent API (Sync/Async) is very interesting:

For example, imagine a 3rd party library that has a method which will retrieve data for us. Since it may be making an asynchronous call to a server, it accepts a callback function, which will be called when the data arrives.

Since, the MyController constructor is always instantiated from within an $apply call, our handler is trying to enter a new $apply block from within one.

I change the code to :

$scope.eventClick = function(event){           
    $timeout(function() {
        $location.path('/event/' + event.id);
    }, 0);
};

Works like a charm !

Here we have used $timeout to schedule the changes to the scope in a future call stack. By providing a timeout period of 0ms, this will occur as soon as possible and $timeout will ensure that the code will be called in a single $apply block.


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


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).


I know it's old question but if you really need use $scope.$applyAsync();


I call $scope.$apply like this to ignored call multiple in one times.

      var callApplyTimeout = null;
      function callApply(callback) {
          if (!callback) callback = function () { };
          if (callApplyTimeout) $timeout.cancel(callApplyTimeout);

          callApplyTimeout = $timeout(function () {
              callback();
              $scope.$apply();
              var d = new Date();
              var m = d.getMilliseconds();
              console.log('$scope.$apply(); call ' + d.toString() + ' ' + m);
          }, 300);
      }

simply call

callApply();

참고URL : https://stackoverflow.com/questions/18626039/apply-already-in-progress-error

반응형