我正在使用textangular作为我正在从事的项目的富文本解决方案。
需要清理此输入,因为我们只允许某些 html 标记。
由于这不是 textangular 的默认可能性,我创建了一个包含 textangular 指令的指令。我可以成功清理输入文本,但视图永远不会更新,我已经没有想法如何实现这一点
指示:
.directive('chTextAngular', function(){
return {
restrict: 'E',
require: 'ngModel',
template: function(elements, attributes) {
return '<div text-angular ng-model="' + attributes.ngModel + '" id="' + attributes.id + '"></div>';
},
link: function(scope, element, attributes, ngModel) {
scope.$watch(attributes.ngModel, function(n, o) {
if(n !== undefined) {
// Replace all the divs with <br> tags
var sanitized = ngModel.$viewValue.replace(/<div>/gm, '<br>').replace(/<\/div>/gm, ' ');
sanitized = sanitized.replace(/<p>/gm, '').replace(/<\/p>/gm, '<br><br>');
sanitized = $.htmlClean(sanitized, {
allowedTags: ["strong", "em", "ul", "ol", "li", "a", "b", "i", "br"],
allowedAttributes: ["a", "href"]
});
console.log(sanitized);
ngModel.$setViewValue(sanitized);
}
});
}
}
});
日志打印出模型,它表明它实际上是变化的。我只是不知道如何在 textangular 中更新实际的 textarea。
任何人都可以帮我解决这个问题,或者让我朝着正确的方向前进吗?