我有一个指令,我在一个div上添加它,以便能够编辑它的内容并将其与作用域进行双向绑定。当我按下回车键时,我希望模糊化,我尝试了如下所示的element[0].blur();
,但这并不起作用。我该怎么做?
.directive("contenteditable", function() {
return {
restrict: "A",
require: "ngModel",
link: function(scope, element, attrs, ngModel) {
function read() {
ngModel.$setViewValue(element.html());
}
ngModel.$render = function() {
element.html(ngModel.$viewValue || "");
};
element.bind("blur keyup change", function() {
scope.$apply(read);
});
element.css('outline','none');
element.bind("keydown keypress", function (event) {
if(event.which === 13) {
element[0].blur();
event.preventDefault();
}
});
}
};
});