Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// Adds `ng-context-menu` for right click support
app.directive("ngContextMenu", function($parse) {
return function(scope, element, attrs) {
var fn = $parse(attrs.ngContextMenu);
element.bind("contextmenu", function(event) {
scope.$apply(function() {
event.preventDefault();
fn(scope, {$event:event});
});
});
};
});
// Adds `contenteditable` with two-way binding
app.directive("contenteditable", ["$sce", function($sce) {
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($sce.getTrustedHtml(ngModel.$viewValue || ""));
};
// Listen for change events to enable binding
element.on("change blur keydown", function(event) {
if (event.keyCode === 13) document.execCommand("insertHTML", false, "<br><br>");
ngModel.$setViewValue(element.decodeHtmlEntities());
if (event.keyCode === 13) return false;
});
element.text(ngModel.$modelValue);
}
};
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.