Skip to content

Instantly share code, notes, and snippets.

@mladenp
Created June 3, 2016 10:00
Show Gist options
  • Save mladenp/748903d9c82b155f7c945bcba285e08a to your computer and use it in GitHub Desktop.
Save mladenp/748903d9c82b155f7c945bcba285e08a to your computer and use it in GitHub Desktop.
Angular directive editable content
.directive('contenteditable', function () {
return {
restrict: 'A', // only activate on element attribute
require: '?ngModel', // get a hold of NgModelController
link: function (scope, element, attrs, ngModel) {
if (!ngModel) return; // do nothing if no ng-model
// Specify how UI should be updated
ngModel.$render = function () {
element.html(ngModel.$viewValue || '');
};
// Listen for change events to enable binding
element.on('blur keyup change', function () {
scope.$apply(readViewText);
});
// No need to initialize, AngularJS will initialize the text based on ng-model attribute
// Write data to the model
function readViewText() {
var html = element.html();
// When we clear the content editable the browser leaves a <br> behind
// If strip-br attribute is provided then we strip this out
if (attrs.stripBr && html == '<br>') {
html = '';
}
ngModel.$setViewValue(html);
}
}
};
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment