简体   繁体   English

使用Angular将文本渲染为html

[英]Rendering text to html with Angular

I am trying to render a text as html using ng-bind as the docs show 我正在尝试使用ng-bind将文本呈现为html,如文档所示

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

The problem is that Angular removes the style attribute and renders this: 问题是Angular删除了style属性并呈现了这个:

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

to this: 对此:

<mark>APPLE</mark>

How to render as html and keep the styles? 如何渲染为HTML并保持样式? I am using Angular version 1.2.6 我使用的是Angular 1.2.6版

You may try this function when you're doing ng-bind-html in your controller 当您在控制器中执行ng-bind-html时,可以尝试此功能

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

Hope this will work 希望这会奏效

$NgSanitizeDocs $ NgSanitizeDocs

First of all include angular-sanitize.js 首先包括angular-sanitize.js

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

Then in the controller, add this method 然后在控制器中添加此方法

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.
   };
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM