【问题标题】:How to use ng-bind-html but disable tag <h1>, <b>... AngularJS如何使用 ng-bind-html 但禁用标签 <h1>、<b>... AngularJS
【发布时间】:2017-11-17 11:22:23
【问题描述】:

我使用 ng-bind-html 并显示成功数据,但我只想显示文本,而不是显示带有 style 的数据,因为它破坏了我的布局:

数据是内容的注释:&lt;h1&gt;some text....&lt;/h1&gt;

我的过滤器:

var myApp = angular.module("myModule", []);
myApp.filter('to_trusted', ['$sce', function ($sce) {
    return function (text) {
        return $sce.trustAsHtml(text);
    };
}]);

我的看法:

<p ng-bind-html="note.content.substr(0, 130) + '...' | to_trusted"></p>

【问题讨论】:

  • 您真的不应该任意信任从其他站点发送的内容,除非您将其过滤掉以确保它没有被发送到您的程序中的恶意脚本。不确定是否要去掉客户端的 html 标签,但服务器端可以使用 unfluff github.com/ageitgey/node-unfluff

标签: angularjs ng-bind-html


【解决方案1】:

简短回答:您可以将 css 类应用于 div,重置覆盖它的 html 标记格式化行为。例如:

<p class="ignore-html-tags" ng-bind-html="note.content.substr(0, 130) + '...' | to_trusted"></p

在你的 CSS 中:

.ignore-html-tags * {
  font-size: 10px !important;
  font-weight: normal !important;
  /* any other style overwrite */
}

【讨论】:

  • 它不起作用 :(。Html:

    aaa

    类 ignore-html-tags 的样式不起作用带有

    演示:jsfiddle.net/lion5893/2t5q0fhc

  • @NamLêQuý 你能发一个 jsfiddle 或 codepen sn-p 吗?我直接帮你上代码,然后
  • 谢谢你的帮助,我修好了。我使用 .ignore-html-tags * {...}
猜你喜欢
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多