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

$digest already in progress #66

Closed jdlaroche closed 9 years ago

jdlaroche commented 10 years ago

The scope.$apply made in the "on change" event does not check if a digest is already in progress. You should wrap it with a check of the $scope.$$phase or use the angular 1.2 $scope.$evalAsync.

Error: [$rootScope:inprog] $digest already in progress http://errors.angularjs.org/1.2.17/$rootScope/inprog?p0=%24digest at http://localhost:40230/bower_components/angular/angular.js:78:12 at beginPhase (http://localhost:40230/bower_components/angular/angular.js:12826:15) at Scope.$get.Scope.$apply (http://localhost:40230/bower_components/angular/angular.js:12615:11) at Scope.ng.config.$provide.decorator.$delegate.proto.$apply (:855:30) at http://localhost:40230/bower_components/angular-ui-codemirror/ui-codemirror.js:71:23 at Array. (http://localhost:40230/bower_components/codemirror/lib/codemirror.js:7154:42) at fireCallbacksForOps (http://localhost:40230/bower_components/codemirror/lib/codemirror.js:1982:21) at endOperation (http://localhost:40230/bower_components/codemirror/lib/codemirror.js:1997:11) at null.setValue (http://localhost:40230/bower_components/codemirror/lib/codemirror.js:2167:17) at null.setValue (http://localhost:40230/bower_components/codemirror/lib/codemirror.js:6683:40)

douglasduteil commented 10 years ago

Which ui-codemirror are you using ? v0.1.6 ?

jdlaroche commented 10 years ago

Yes. I don't know if it is related, but I use the directive twice in my view. Exception is thrown at line 71 of ui-codemirror.js of version 0.1.6

boutils commented 10 years ago

Same issue here with angularjs 1.3 and ui-codemirror 0.1.6

$apply is already in progress in the onChange event....

jdlaroche commented 10 years ago

boutils, I solved the issue by replacing the $apply by a $evalAsync and it worked. I didn't notice any issue (beside losing support of older versions of angular) but I am not 100% it does exactly the same thing... That might be a temprary solution until a new version is available.

esjewett commented 10 years ago

I'm having this issue as well. ui-codemirrror 0.1.6, codemirror 0.4.5, angular 1.3. We do have more than one Codemirror on this page (using YASQE, which embeds it's own Codemirror).

johannesherr commented 9 years ago

We have the same exception with version 0.1.6, angular 1.2.25 and codemirror 4.6.0.

In our case the problem is triggered by windows line endings in a string that is written to the scope element that ui-codemirror is bound to. Changing the scope changes the codemirror input which triggers the "change" ui-codemirror listener which mistakenly thinks it needs to update the scope.

ui-codemirror tries to determine if a change has been made in codemirror that needs to be reflected in the angular model by comparing in line 69:

if (newValue !== ngModel.$viewValue) {

In this case newValue does not have windows line endings (which have been removed by CodeMirror.getValue() (line 6385)). Angular does not modify the string so it still has the windows line endings which results in the above condition to be true (the two strings differ only in line endings). As a result the ui-codemirror logic is triggered which calls $apply even though the method was triggered by a model change already which causes the error.

Other people also have had problems with CodeMirror changing line breaks: https://github.com/adobe/brackets/issues/164