从字符串中渲染html标记

时间:2016-11-08 06:35:27

标签: javascript jquery angularjs

我在JSON中有一些像1000, 2000, <b>3000</b>, <4000>, <b>5000</b>这样的值作为API响应。我想在表中呈现此响应。所以我尝试了ng-bind-html。但它只显示具有3000,5000等标签的值。我想显示所有值,1000,2000,4000为纯字符串,3000,5000为BOLD /或任何其他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>

3 个答案:

答案 0 :(得分:1)

您可以使用ng-bind-html-unsafe#this is ParentA class ParentA(Base): __tablename__ = 'parenta' id = Column(Integer, primary_key=True) name = Column(String(12)) need_delete = Column(Integer) children = relationship("ChildAA", back_populates="parent") #this is ChildAA class ChildAA(Base): __tablename__ = 'childaa' name = Column(String(12)) id = Column(Integer, primary_key=True) need_delete = Column(Integer) parenta_id = Column(Integer, ForeignKey('parenta.id')) parenta = relationship("ParentA") #this is ParentB ........ 。但请注意这里的安全问题。

您可以找到更多详情here

确保清理字符串,以防止出现安全漏洞

答案 1 :(得分:1)

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

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

答案 2 :(得分:1)

你可以使用ng-sanitize模块 - see here

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