我有一个指令contentEditable
,它也引用html5 attr
::docs::。
<p contentEditable="{{edit}}" ng-model="pages.title">default</p>
在指令的 dblclick 上,我希望 contentEditable 的值切换到true
;在模糊切换到false
..
但是,我一直在尝试,双击拒绝正常工作。不确定如何使它按照我想要的方式运行。
查看 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();
}
});
}
};
});