请找到plnkr
我想要显示一些html预览。html已经在服务器上进行了清理(例如:"<b>HELLO</b>")。如何显示html表单。在本例中,我希望显示myHtml的myHtml2 (第一次预览)。
html
 <div ng-controller="myController">
      <div ng-bind-html="myHtml"></div>
      <div ng-bind-html="myHtml2"></div>
      <div >{{myHtml2}}</div>
    </div>js
myApp.controller('myController', ['$scope', '$sce', function myController($scope, $sce){
  $scope.myHtml = "<b>HELLO</b>";
  $scope.myHtml2 = "<b>HELLO</b>";      
}]);输出
HELLO
<b>HELLO</b>
<b>HELLO</b>发布于 2013-12-14 01:58:34
您只需要在客户端上使用$sce.trustAsHtml和unsanitize HTML:http://plnkr.co/edit/h2loxWsPJOELhNvkfHmK?p=preview
// From: https://stackoverflow.com/questions/1912501/unescape-html-entities-in-javascript
function htmlDecode(input){
  var e = document.createElement('div');
  e.innerHTML = input;
  return e.childNodes.length === 0 ? "" : e.childNodes[0].nodeValue;
}
myApp.controller('myController', ['$scope', '$sce', function myController($scope, $sce){
  $scope.myHtml = "<b>HELLO</b>";
  $scope.myHtml2 = $sce.trustAsHtml(htmlDecode("<b>HELLO</b>"));
}]);来自:Unescape HTML entities in Javascript?的htmlDecode
但是,我不建议采用这种方法。这感觉非常黑客,我怀疑可能会导致您的网站上的漏洞。
https://stackoverflow.com/questions/20572947
复制相似问题