angularJS如何忽略某些HTML标签?

angularJS如何忽略某些HTML标签?,第1张

angularJS如何忽略某些HTML标签?

您可以创建过滤器,以清理HTML。

我在其中使用了strip_tags函数
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 = /<!--[sS]*?-->|<?(?:php)?[sS]*??>/gi;      return input.replace(commentsAndPhpTags, '')        .replace(tags, function(
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>'));  });}]);
, ) { return allowed.indexOf('<' + .toLowerCase() + '>') > -1 ?
<div ng-bind-html="sanitized"></div>
: ''; }); }});

控制器

视图

http://plnkr.co/edit/qOuvpSMvooC6jR0HxCNT?p=preview



欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5008109.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-14
下一篇2022-11-15

发表评论

登录后才能评论

评论列表(0条)

    保存