I have a contenteditable directive with placeholder which based on Craig Stuntz's javascript contenteditable placeholder. This directive does the following job
- Check if div textContent exist, if exists hide placeholder else show placeholder
- If user focus on contenteditable then hide placeholder
But I have a problem which already mentioned by 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.
I have no idea where to put .triggerHandler('change') and how the directive know if div text from javascript is empty or not empty?
Below is the code
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;
}