Error calling digest

0

My js file:

$scope.loadOptions = function(email) {
   //aqui llamo a varios métodos y al final tengo voy asignando valores a variables
   $scope.$apply();
}

my html file:

<div ng-init="loadOptions">
  <textarea class="w95" name="tinymce${it?.id}" id="tinymce${it?.id}">{{qualitative[${it.id}]}}</textarea>
</div>

I get this error:

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

The value of the textarea is sometimes shown to me and sometimes not, I do not know if it is related that I have to update the values

    
asked by sirdaiz 10.07.2017 в 14:51
source

3 answers

2

$ apply forcing the execution of a $ digest () below . The ngClick directive already has a $apply() event running and you can not run 2 $digest() at the same time.

You should only use $apply() when you are outside the context of angularjs. Digify outside the controller or in an external code:

$("#button").click(function(){
  angular.element($("#mi-otro-boton-dentro-de-un-controlador").scope().$apply();
});

The jquery click event is outside the context of angularjs and does not execute $apply() , so when executing $apply we manually forced angular to update the view using $digest() .

In summary. Remove $scope.$apply() since ngClick is running it.

    
answered by 10.07.2017 в 15:26
0

The problem you're having is a bit related to the functioning of Javascript, another bit to how Angular works. As Einer says, the $ apply is invoked by Angular in all cases, and if you try to call $ apply in another $ apply, throw away the error you're having.

In some cases it is necessary to call back the $ apply since Angular can not detect changes that are outside of its scope.

One solution for these cases is to force Javascript to generate another "round" or "shift" of execution.

In my case I have had situations in which I needed to force a refresh and for those cases, in angular 1 and 2, it has helped me to do this:

  setTimeout(function() {
    $scope.$apply(function() {
      $scope.name = "Timeout called!";
    });
  }, 0);

This note ( link ) helped me a lot to understand when and how to use $ apply in a way that does not become an anti-pattern in your application.

Here is an example of this applied theory: link

In the example you will be able to see several things, when it is updated and when it is not.

Just one more clarification, always before applying this type of solutions, first check your logic, in my case, in 8 out of 10, I found another solution that fixed the problem.

Second clarification, instead of setTimeout, it is convenient to use $ timeout , which already includes a $ apply in the code, but in the answer I wanted to demonstrate a bit the reason for the error and how to solve it in accordance to its causes.

I hope it is of your use, otherwise, do not hesitate to ask again.

    
answered by 10.07.2017 в 22:07
0

The safest way to apply a $scope.$apply(); is to do it within a $timeout . Something like this:

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

This way you will not get the error you describe.

    
answered by 11.07.2017 в 19:45