简体   繁体   English

如何在AngularJS中将文本呈现为HTML

[英]How to render text into HTML in AngularJS

I have a variable in a scope with some HTML content. 我在范围内有一个带有一些HTML内容的变量。 I want to render it as HTML on the webpage, but in my case it displays as full text. 我想在网页上将其呈现为HTML,但在我的情况下,它显示为全文。 Can anyone help me? 谁能帮我?

This is my code:- 这是我的代码: -

//contoller.js //contoller.js

$scope.message = '<b><i>result has been saved successfully.</i></b>';

//demo.html //demo.html

<p ng-bind="message"></p>

You need to inject $sce service into your controller or Directive etc. and use $sce service like this:- 您需要将$sce服务注入您的controllerDirective等,并使用这样的$sce service : -

$scope.Message = $sce.trustAsHtml("<b><i>result has been saved successfully.</i></b>");

And bind this in your HTML page eg; 并在HTML页面中绑定它,例如;

<p ng-bind-html = "Message"></p>

You have to secure your content with the $sce service and then use the 您必须使用$ sce服务保护您的内容,然后使用

ng-bind-html directive ng-bind-html指令

docs here . docs在这里


EDIT 编辑

you can find the usage of sce.trustAsHtml here . 你可以在这里找到sce.trustAsHtml的用法。

<p ng-bind-html="message"></p>

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

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