我有一个带占位符的contenteditable指令,它基于Craig Stuntz's javascript contenteditable placeholder。该指令做以下工作将div文本传递给angularjs指令
- 检查DIV的textContent存在,如果存在隐藏占位否则显示占位
- 如果用户注重CONTENTEDITABLE然后隐藏占位
但是我有一个已经提到的一个问题由Craig特技
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;
}
你有可能发布最终解决方案吗? – chris