获取AngularJS错误:" [$ rootScope:inprog] $已经消化进度"无需手动$申请 [英] Getting AngularJS Error: "[$rootScope:inprog] $digest already in progress" without a manual $apply

查看:3803
本文介绍了获取AngularJS错误:" [$ rootScope:inprog] $已经消化进度"无需手动$申请的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此错误的其他帖子始终包括有人试图$应用不使用安全的应用,但是这不是我的情况为例。我的作用是返回成功从我的API请求的数据,但我不能清除这个bug,它的驾驶我坚果。
在.success之前每次被称为我的$ HTTP的功能我得到错误:[$ rootScope:inprog] $消化正在进行中在控制台中。下面是我的控制器和服务。谢谢!

Other posts on this error always include someone trying to $apply without using a safe apply, but that's not the case in my example. My function IS successfully returning the data I requested from the API, but I can't clean this bug and it's driving me nuts. Every time before the .success is called in my $http function I get "Error: [$rootScope:inprog] $digest already in progress" in the console. Below are my controller and service. Thanks!

下面是我的服务,包括功能后有效载荷的$ HTTP调用:

Here's my service including a function to post an $http call with a payload:

Services.service( 'CoolService', ['$q', '$rootScope', '$http', 'Auth', function($q, $rootScope, $http, Auth){
var service = {
    create: function(payload){
        var deferred = $q.defer();
        $http({
            'url': '/api/endpoint/',
            'dataType':'json',
            'method': 'POST',
            data: payload
        }).success(function(data,status, headers, config){
            deferred.resolve(data);

        })
        .error(function(data, status, headers, config){
            deferred.reject("Error in request.");
        });
        return deferred.promise;
        }
    }
    return service;
}]);

这是我的控制器,它调用服务:

And here's my controller which calls the service:

controllers.controller('CoolCtrl',['$scope', '$modal', '$log','CoolService', function($scope, $modal, $log, CoolService){
    getCoolData = function (input_data) {
        CoolService.create(input_data).then(function(results){
            new_cool = results.results;
        }, function(error){
        console.log("there was an error getting new cool data");
        });
    };
    var payload = {
        user_id: data1,
        cool_id: data2,
    }
    var new_cool_data = getCoolData(payload);
    console.log(new_cool_data);
}]);

下面VAR new_cool_data日志被异步操作之前调用,但new_cool并得到内getCoolData分配。那么语句中。
任何帮助摆脱这个bug或使得总体上不蹩脚会大大AP preciated!

The log below var new_cool_data gets called before the async operation, but new_cool does get assigned inside the .then statement within getCoolData. Any help getting rid of this bug or making it not crappy in general would be greatly appreciated!

下面就是全部的错误:
<一href=\"https://gist.github.com/thedore17/bcac9aec781ef9ba535b\">https://gist.github.com/thedore17/bcac9aec781ef9ba535b

推荐答案

添加这个小方法,并调用它的就地适用()

Add this little method and call it inplace of apply()

function CheckScopeBeforeApply() {
    if(!$scope.$$phase) {
         $scope.$apply();
    }
};

这篇关于获取AngularJS错误:&QUOT; [$ rootScope:inprog] $已经消化进度&QUOT;无需手动$申请的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆