angularJS我怎么能忽略某些HTML标签? [英] angularJS How can I ignore certain HTML tags?
问题描述
我得到这个错误,因为其中一个用户在他的文章中加入 3;
I got this error because one of the users added in his post <3
错误:[$ sanitize方法:badparse]的消毒剂无法解析HTML以下块:3;
Error: [$sanitize:badparse] The sanitizer was unable to parse the following block of html: <3
我写了code,它 NG-绑定-HTML =Detail.details
我要他只能采取&LT; A&GT;
标记和标记&LT; BR /&GT;
I want him to be taken only <a>
tag and tag <br />
这可能吗?
感谢您!
推荐答案
您可以创建过滤器,将净化你的HTML。
You can create filter which will sanitize your html.
我用它用strip_tags功能
http://phpjs.org/functions/strip_tags/
I used in it strip_tags function http://phpjs.org/functions/strip_tags/
angular.module('filters', []).factory('truncate', function () {
return function strip_tags(input, allowed) {
allowed = (((allowed || '') + '')
.toLowerCase()
.match(/<[a-z][a-z0-9]*>/g) || [])
.join(''); // making sure the allowed arg is a string containing only tags in lowercase (<a><b><c>)
var tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>/gi,
commentsAndPhpTags = /<!--[\s\S]*?-->|<\?(?:php)?[\s\S]*?\?>/gi;
return input.replace(commentsAndPhpTags, '')
.replace(tags, function($0, $1) {
return allowed.indexOf('<' + $1.toLowerCase() + '>') > -1 ? $0 : '';
});
}
});
控制器:
angular.module('myApp', ['filters'])
.controller('IndexController', ['$scope', 'truncate', '$sce', function($scope, truncate, $sce){
$scope.text="";
$scope.$watch('text', function(){
$scope.sanitized = $sce.trustAsHtml(truncate($scope.text, '<a><br>'));
});
}]);
查看:
<div ng-bind-html="sanitized"></div>
<一个href=\"http://plnkr.co/edit/qOuvpSMvooC6jR0HxCNT?p=$p$pview\">http://plnkr.co/edit/qOuvpSMvooC6jR0HxCNT?p=$p$pview
这篇关于angularJS我怎么能忽略某些HTML标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!