angularJS How can I ignore certain HTML tags?(angularJS 如何忽略某些 HTML 标签?)
问题描述
我收到此错误是因为其中一位用户在他的帖子中添加了 <3
I got this error because one of the users added in his post <3
错误:[$sanitize:badparse] sanitizer 无法解析以下 html 块:<3
Error: [$sanitize:badparse] The sanitizer was unable to parse the following block of html: <3
我写的代码 ng-bind-html ="Detail.details"
我要他只取<a>
标签和<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]*)[^>]*>/gi,
commentsAndPhpTags = /<!--[sS]*?-->|<?(?:php)?[sS]*??>/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>
http://plnkr.co/edit/qOuvpSMvooC6jR0HxCNT?p=preview
这篇关于angularJS 如何忽略某些 HTML 标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:angularJS 如何忽略某些 HTML 标签?


基础教程推荐
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01