【问题标题】:ng-bind-html doesnt works properly for html tagsng-bind-html 不适用于 html 标签
【发布时间】:2015-07-30 16:35:37
【问题描述】:

我已经关注this topic

有效!从此

<p>9+555</p>\r\n

到这个<p>9+555</p>

它仍然将 HTML 标记留在那里。

如何使这些标签作为 HTML 而不是文本。 非常感谢

【问题讨论】:

    标签: javascript angularjs html


    【解决方案1】:

    你不应该使用 &lt 和 &gt 你应该直接写<p>

    angular.module('bindHtmlExample', ['ngSanitize'])
        .controller('ExampleController', ['$scope', function($scope) {
            $scope.myHTML = 'I am an <code>HTML</code>string with ' +
                            '<a href="#">links!</a> and other <em>stuff</em>';
        }]);
    

    这是一个 plunk,显示了您的字符串与正确 html 格式的字符串之间的差异 http://plnkr.co/edit/RZmuzTilvBkU3oGUre7J

    【讨论】:

    • 能否解释一下为什么我们不应该使用 &lt 和 &gt?
    • 我基本上将“

      9+555

      ”存储到数据库中,但那些“&lt,&gt”不知从何而来
    • 它们来自您的服务器。问题是如何将该字符串放入 JS 文件中。
    • 你不应该使用 <和 $gt;因为它们是 HTML entities 并且在被解析为 HTML 时将被解释为字符串。
    • @ChanonDeeprasertkul 我在我的答案中添加了一个 plunk,以便您更轻松地看到差异。
    猜你喜欢
    • 2023-03-15
    • 2014-09-19
    • 2014-02-06
    • 2014-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-09
    • 1970-01-01
    相关资源
    最近更新 更多