【发布时间】:2014-09-12 22:52:02
【问题描述】:
我有一个名为 contentEditable 的指令,它也引用了 html5 attr ::docs::。
<p contentEditable="{{edit}}" ng-model="pages.title">default</p>
在指令的 dblclick 上,我希望 contentEditable 的值切换到 true;在模糊切换到false..
我一直在尝试,但是双击无法正常工作。 不确定如何使它按我想要的方式运行。
check out the JSfiddle
var app = angular.module('morningharwoodApp');
app.directive('contenteditable', function() {
return {
require: 'ngModel',
scope: {
edit: '@'
},
controller: function($scope) {
$scope.edit = false;
console.log($scope.edit);
},
link: function(scope, elm, attrs, ctrl) {
// view -> model
elm.bind('blur', function() {
scope.$digest(function() {
ctrl.$setViewValue(elm.html());
});
});
// model -> view
ctrl.render = function(value) {
elm.html(value);
};
// load init value from DOM
ctrl.$setViewValue(elm.html());
elm.bind('dblclick', function(event) {
scope.edit = !scope.edit;
console.log(scope.edit);
console.log('keydown ' + event.which);
var esc = event.which === 27,
enter = event.which === 13,
el = event.target;
if (esc || enter) {
// console.log('esc');
ctrl.$setViewValue(elm.html());
scope.editable = false;
el.blur();
event.preventDefault();
}
});
}
};
});
【问题讨论】:
-
虽然相当混乱,但代码似乎在 Chrome 上运行良好。有什么问题?
-
双击确实有效,但单击后直接进入可编辑状态。这很奇怪.. html5 中的 contentEditable 有一个 onclick 事件......我不知道这是否可能:c
-
所以你想只在双击打开编辑模式是的?
-
@MatthewHarwood:当然是大声笑。
contenteditable是一个 HTML5 属性。您应该为它使用不同的名称。
标签: javascript html angularjs angularjs-scope angular-directive