<!DOCTYPE html>
<html ng-app="test">
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<head>
<title></title>
<!-- Core AngularJs -->
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.3/angular.min.js"></script>
<!-- Code Mirror -->
<script src="http://codemirror.net/lib/codemirror.js" type="text/javascript"></script>
<link href="http://codemirror.net/lib/codemirror.css" rel="stylesheet" type="text/css">
<link href="http://codemirror.net/theme/monokai.css" rel="stylesheet" type="text/css">
<!-- Main App -->
<script src="app.js" ></script>
</head>
<body>
<div>
<div>Output #1 works :: {{codeMirrorModel}}</div>
<textarea ui-codemirror="{theme: 'monokai'}" ng-model="codeMirrorModel"></textarea>
<div>Output #2 doesn't work :: {{codeMirrorModel}}</div>
</div>
<div>Output #3 works :: {{codeMirrorModel}}</div>
</body>
</html>
var app = angular.module("test", ["ui.directives"]);
/**
* Slightly cutdown code from AngularUI
*/
angular.module('ui.config', []).value('ui.config', {});
angular.module('ui.directives', ['ui.config']);
angular.module('ui.directives').directive('uiCodemirror', ['ui.config', '$parse', function (uiConfig, $parse) {
uiConfig.codemirror = uiConfig.codemirror || {};
return {
require: 'ngModel',
link: function (scope, elm, attrs, ngModel) {
var codemirror;
// This is the method that we use to get the value of the ui-codemirror attribute expression.
var uiCodemirrorGet = $parse(attrs.uiCodemirror);
// This method will be called whenever the code mirror widget content changes
var onChangeHandler = function (ed) {
// We only update the model if the value has changed - this helps get around a little problem where $render triggers a change despite already being inside a $apply loop.
var newValue = ed.getValue();
if (newValue !== ngModel.$viewValue) {
ngModel.$setViewValue(newValue);
scope.$apply();
}
};
// Create and wire up a new code mirror widget (unwiring a previous one if necessary)
var updateCodeMirror = function (options) {
// Merge together the options from the uiConfig and the attribute itself with the onChange event above.
options = angular.extend({}, options, uiConfig.codemirror);
// If there is a codemirror widget for this element already then we need to unwire if first
if (codemirror) {
codemirror.toTextArea();
}
// Create the new codemirror widget
codemirror = CodeMirror.fromTextArea(elm[0], options);
// As of CodeMirror 3.0+ we need to explicitly register the onChange listener
codemirror.on("change", onChangeHandler);
};
// Initialize the code mirror widget
updateCodeMirror(uiCodemirrorGet());
// Now watch to see if the codemirror attribute gets updated
scope.$watch(uiCodemirrorGet, updateCodeMirror, true);
// CodeMirror expects a string, so make sure it gets one.
// This does not change the model.
ngModel.$formatters.push(function (value) {
if (angular.isUndefined(value) || value === null) {
return '';
}
else if (angular.isObject(value) || angular.isArray(value)) {
throw new Error('ui-codemirror cannot use an object or an array as a model');
}
return value;
});
// Override the ngModelController $render method, which is what gets called when the model is updated.
// This takes care of the synchronizing the codeMirror element with the underlying model, in the case that it is changed by something else.
ngModel.$render = function () {
codemirror.setValue(ngModel.$viewValue);
};
}
};
}]);