Skip to content

Instantly share code, notes, and snippets.

@aruss
Created April 3, 2015 22:51
Show Gist options
  • Save aruss/5ba0fa16b0045369617c to your computer and use it in GitHub Desktop.
Save aruss/5ba0fa16b0045369617c to your computer and use it in GitHub Desktop.
'use strict';
(function(app, $, undefined) {
app.directive('uiEditable', function($sce) {
return {
restrict: 'A',
require: '?ngModel',
link: function(scope, element, attrs, ngModel) {
if (!ngModel) {
return;
}
ngModel.$render = function() {
return element.html(ngModel.$viewValue);
};
element.bind('blur keyup change', function() {
if (ngModel.$viewValue !== $.trim(element.html())) {
return scope.$apply(read);
}
});
function read() {
return ngModel.$setViewValue($.trim(element.html()));
};
}
};
});
})(angular.module('app'), angular.element);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment