用Angular把文本渲染成html

Rendering text to html with Angular

本文关键字:html 文本 Angular      更新时间:2023-09-26

我正在尝试使用ng-bind将文本渲染为html作为文档显示

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

问题是,Angular删除了style属性,呈现如下:
"<mark style='background-color:#FF9DFF;'>APPLE</mark>."

:

<mark>APPLE</mark>

如何呈现为html和保持样式?我使用的是Angular 1.2.6版本

您可以在控制器中执行ng-bind-html时尝试此函数

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

希望这将工作

美元NgSanitizeDocs

首先包含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.
   };
})