.directive("contenteditable", function() {
return {
restrict: "A",
require: "ngModel",
link: function(scope, element, attrs, ngModel) {
function read() {
ngModel.$setViewValue(element.html());
}
ngModel.$render = function() {
element.html(ngModel.$viewValue || "");
};
element.bind("blur keyup change", function() {
scope.$apply(read);
});
}
};
})
It is functioning well when implemented in this way.
Initially, I used the following in the directive link function: link: function(scope, elm, attr, ngModel) {
function updateViewValue() {
ngModel.$setViewValue(this.innerHTML);
}
//Alternatively, you can bind it to other events
elm.on('keyup', updateViewValue);
scope.$on('$destroy', function() {
elm.off('keyup', updateViewValue);
});
ngModel.$render = function() {
elm.html(ngModel.$viewValue);
}
}
However, when using this inside the directive link function, the changes made with ng-change were not reflecting on the front end.