我有一个基于Craig Stuntz 的 javascript contenteditable placeholder 的带有占位符的 contenteditable 指令。该指令执行以下工作
- 检查 div textContent 是否存在,如果存在则隐藏占位符,否则显示占位符
- 如果用户关注 contenteditable 然后隐藏占位符
但我有一个 Craig Stunt 已经提到的问题
If you update the div text in JavaScript (instead of just typing into the div on the page), no events are fired, and you must let the plugin know that you've changed the contents by triggering the change event.
我不知道该放在哪里.triggerHandler('change')
以及指令如何知道来自 javascript 的 div 文本是否为空?
下面是代码
app.directive("contenteditable", function() {
return {
require: 'ngModel',
link: function(scope, element, attrs, ctrl) {
// view -> model
element.bind('input', function() {
scope.$apply(function() {
ctrl.$setViewValue(element.text());
});
if (this.textContent) {
this.setAttribute('data-contenteditable-placeholder', 'true');
console.log(this);
} else {
this.removeAttribute('data-contenteditable-placeholder');
}
});
// model -> view
//ctrl.$render = function() {
// element.text(ctrl.$viewValue);
//};
//ctrl.$setViewValue(element.text());
}
}
});
CSS
*[data-placeholder]:not(:focus):not([data-contenteditable-placeholder])::before {
content: attr(data-placeholder);
margin-left: 2px;
color: #b3b3b3;
}
div[contenteditable]:focus{
outline: none;
}