【问题标题】:Rendering text to html with Angular使用 Angular 将文本渲染为 html
【发布时间】:2014-03-14 14:49:47
【问题描述】:

我正在尝试使用 ng-bind 作为 docs show 将文本呈现为 html

<div ng-bind-html="text"></div>

问题在于 Angular 移除了 style 属性并呈现了这个:

"<mark style='background-color:#FF9DFF;'>APPLE</mark>."

到这里:

<mark>APPLE</mark>

如何呈现为 html 并保留样式? 我正在使用 Angular 1.2.6 版

【问题讨论】:

  • 请注意 $sce 是 v1.2 及以上版本。

标签: javascript html angularjs html-rendering


【解决方案1】:

您可以在控制器中使用ng-bind-html 时尝试此功能

$sce.trustAsHtml('myHTML'); //$sce would be the parameter with $scope in module

希望这会奏效

$NgSanitizeDocs

【讨论】:

    【解决方案2】:

    首先包括angular-sanitize.js

    <div ng-bind-html="deliberatelyTrustDangerousSnippet()"></div>
    

    然后在控制器中,添加这个方法

    pk.controller("createBlog", function($scope, $sce){
       //ace needs to be injected in the controller.
       $scope.deliberatelyTrustDangerousSnippet = function() {
           return $sce.trustAsHtml($scope.htmlcontent); //html content is th binded content.
       };
    })
    

    【讨论】:

      猜你喜欢
      • 2012-10-31
      • 2017-12-26
      • 2021-01-05
      • 1970-01-01
      • 2020-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多