Vipul sharma
Vipul sharma

Reputation: 1255

Getting error while calling angularjs function

while calling function getting this error

 Error: "[$rootScope:inprog] http://errors.angularjs.org/1.6.9/$rootScope/inprog?p0=%24digest"

But if calling it from console it is working fine.

angular.element('body').scope().set_active_counter(1);

And function looks like this

    $scope.set_active_counter = function (i) {
        $scope.$apply(function () {
            $scope.active_question_counter = i;
        });
        active_question_counter =  i;
    }

Upvotes: 0

Views: 31

Answers (1)

CodeMonkey
CodeMonkey

Reputation: 419

"inprog" typically means there's a digest in progress. Calling $scope.$apply() manually is the cause. The only time you should ever need to manually call $scope.$apply() is if it's inside some kind of async-ish call like the callback from $http or a promise.

Assigning a value to the $scope variable "active_question_counter" should automatically trigger a digest. But because you have manually triggered the digest, and within the scope of that digest you are making the change, the result is a "digest already in progress" error.

You shouldn't need to explicitly call $apply() here unless there's some weird behind-the-scenes linking going on that doesn't actively detect that something has changed. If that is the case, a hacky workaround is to wrap the $scope.$apply(...) inside a setTimeout().

I'm not really sure why executing this through angular.elment().scope() in the console works. Maybe the console is running in a separate context?

Upvotes: 1

Related Questions