angular-ui / ui-codemirror

This directive allows you to add CodeMirror to your textarea elements.
http://angular-ui.github.io/ui-codemirror
MIT License
378 stars 193 forks source link

Error: [$rootScope:inprog] digest in progress. Fix #73

Closed znap026 closed 9 years ago

znap026 commented 9 years ago

I had to add if(!scope.$$phase) {

} Around the $apply on line 88 to stop Error: [$rootScope:inprog] digest in progress. I think it was due to updating multiple code mirrors. This definitely fixed my problem and i can't think of any harm in adding it.

43081j commented 9 years ago

It is fairly easy to cause a digest error with this module, it would all be fixed by using $applyAsync I imagine.

Do not use $$phase as it is made to be a private variable (as is anything starting with $$).

deblockt commented 9 years ago

I have the same issue. You need to use $timeout service for execute $apply function. You must not do :

codeMirror.on('change', function (instance) {
    var newValue = instance.getValue();
    if (newValue !== ngModel.$viewValue) {
         // Changes to the model from a callback need to be wrapped in $apply or angular will not notice them
        scope.$apply(function () {
           ngModel.$setViewValue(newValue);
        });
   }
});

but do something like this :

codeMirror.on('change', function (instance) {
    var newValue = instance.getValue();
    if (newValue !== ngModel.$viewValue) {
         // Changes to the model from a callback need to be wrapped in $apply or angular will not notice them
        $timeout(function(){
            scope.$apply(function () {
                 ngModel.$setViewValue(newValue);
            });
        });
   }
});
43081j commented 9 years ago

As mentioned previously, the correct solution would be to use $applyAsync such that it will apply the changes in the next digest, rather than trying to trigger one immediately.