【问题标题】:How to display HTML from response as HTML in ng-repeat如何在 ng-repeat 中将响应中的 HTML 显示为 HTML
【发布时间】:2018-06-11 07:22:01
【问题描述】:

我有 n 条来自后端的记录作为数组中的 HTML。我需要在我的视图中将 HTML 响应显示为 HTML。我试过 ng-bind-html ,但它取最后一个值。需要帮助。

  $scope.res=  "data": [
            {
              "jd": "<p>this jd1</p>"
            },
            {
              "jd": "<li>this jd2</li>"
            },
            {
              "jd": "<ul>this jd3</ul>"
            },
            {
              "jd": "<p>this jd4</p>"
            }
          ]
        }

HTML:

 <div ng-repeat="item in res">
    <div ng-bind-html ="item.jd">
    </div>

【问题讨论】:

    标签: angularjs ng-bind-html


    【解决方案1】:

    您可以使用 $sce.trustAsHtml。请参阅文档here

    你可以做的是:

    在你的控制器中添加这一行:

    $scope.trustAsHtml = $sce.trustAsHtml;
    

    并像这样更新您的 HTML:

    <div ng-bind-html ="trustAsHtml(item.jd)">
    

    请注意,您可能应该开始使用 Angular 6 而不是 AngularJS

    【讨论】:

    • 嗨,一定会的。我还有一个问题。我正在编码并将html响应发送到后端。现在我需要解码它们并在这里使用它。所以我写了一个过滤器。解码过滤器:app.filter('decodeFilter', function() { return function(input) { return decodeURIComponent(unescape(input)); }; });
    猜你喜欢
    • 2019-02-16
    • 2012-11-02
    • 1970-01-01
    • 2016-05-29
    • 2020-02-20
    • 1970-01-01
    • 2015-12-03
    • 2019-06-09
    • 2017-07-13
    相关资源
    最近更新 更多