Skip to content
This repository was archived by the owner on May 25, 2019. It is now read-only.

Commit 5cdb98f

Browse files
committed
Merge pull request #54 from whitehat101/on-change-apply
scope.$apply and $digest already in use
2 parents 18317b7 + 1d46946 commit 5cdb98f

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

src/ui-codemirror.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,6 @@ angular.module('ui.codemirror', [])
6161
if (angular.isDefined(scope.$eval(iAttrs.uiCodemirror))) {
6262
scope.$watch(iAttrs.uiCodemirror, updateOptions, true);
6363
}
64-
// Specialize change event
65-
codeMirror.on('change', function (instance) {
66-
var newValue = instance.getValue();
67-
if (ngModel && newValue !== ngModel.$viewValue) {
68-
ngModel.$setViewValue(newValue);
69-
}
70-
if (!scope.$$phase) {
71-
scope.$apply();
72-
}
73-
});
7464

7565

7666
if (ngModel) {
@@ -95,6 +85,18 @@ angular.module('ui.codemirror', [])
9585
var safeViewValue = ngModel.$viewValue || '';
9686
codeMirror.setValue(safeViewValue);
9787
};
88+
89+
90+
// Keep the ngModel in sync with changes from CodeMirror
91+
codeMirror.on('change', function (instance) {
92+
var newValue = instance.getValue();
93+
if (newValue !== ngModel.$viewValue) {
94+
// Changes to the model from a callback need to be wrapped in $apply or angular will not notice them
95+
scope.$apply(function() {
96+
ngModel.$setViewValue(newValue);
97+
});
98+
}
99+
});
98100
}
99101

100102

0 commit comments

Comments
 (0)