【问题标题】:Render html tag from string从字符串渲染 html 标签
【发布时间】:2017-03-21 16:43:16
【问题描述】:

我在 JSON 中有一些值,例如 1000, 2000, <b>3000</b>, <4000>, <b>5000</b> 作为 API 响应。我想在表格中呈现这个响应。所以我尝试了 ng-bind-html。但它只显示具有 3000,5000 等标签的值。我想显示所有值,1000,2000,4000 为纯字符串,3000,5000 为粗体/或任何其他 HTML 标记。

angular.forEach($scope.arr2.test,function(item)
  $scope.res=$sce.trustAsHtml(item.amount);
  return $scope.res;
}); 

在 HTML 方面,我有这样的东西

<td id="price" class="edit" ng-repeat="pro in d.procedure" ng-bind-html="valueCheck(d._id,pro._id,hos._id)"></td>

【问题讨论】:

标签: javascript jquery angularjs


【解决方案1】:

您可以为此使用ng-bind-htmlng-bind-html-unsafe。但请注意这里的安全问题。

您可以找到更多详情here

请务必清理字符串,以防止安全漏洞

【讨论】:

  • 注入 Sanitize 后,ng-bind-html 给我的结果与 $sce.trustAsHtml(item.amount); 相同。意味着它只显示具有 HTML 标签的值,并且 ng-bind-html-unsafe 没有给我任何价值
【解决方案2】:

首先你需要下载 ng-sanitize js https://docs.angularjs.org/api/ngSanitize

然后将 ng-sanitize 注入 Angular 模块。 那么你可以使用 ng-bind-html 和 ng-bind-html-unsafe

【讨论】:

    【解决方案3】:

    您也可以使用 ng-sanitize 模块 - see here

     var app = angular.module("myApp", ['ngSanitize']);
    

    【讨论】:

      猜你喜欢
      • 2019-07-19
      • 1970-01-01
      • 2021-01-30
      • 1970-01-01
      • 1970-01-01
      • 2018-08-30
      • 2019-11-26
      • 2011-09-17
      • 2016-10-01
      相关资源
      最近更新 更多