一尘不染

$ apply已在执行中错误

angularjs

堆栈跟踪:

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上它会抛出上述错误。有什么想法怎么了?


阅读 177

收藏
2020-07-04

共1个答案

一尘不染

因为$apply在现有的消化周期内进行调用,所以收到此错误。

最大的问题是:您为什么打电话$apply$apply除非您是从非Angular事件进行接口的,否则您根本不需要调用。$apply通常的存在意味着我做错了事(除非,再次,$
apply来自非Angular事件)。

如果$apply确实合适,请考虑使用“安全应用”方法:

https://coderwall.com/p/ngisma

2020-07-04