angular-ui/ui-codemirror

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

znap026 opened this issue · 3 comments

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.

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 $$).

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);
            });
        });
   }
});

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.