最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to render text into HTML in AngularJS - Stack Overflow

programmeradmin2浏览0评论

I have a variable in a scope with some HTML content. I want to render it as HTML on the webpage, but in my case it displays as full text. Can anyone help me?

This is my code:-

//contoller.js

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

//demo.html

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

I have a variable in a scope with some HTML content. I want to render it as HTML on the webpage, but in my case it displays as full text. Can anyone help me?

This is my code:-

//contoller.js

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

//demo.html

<p ng-bind="message"></p>
Share Improve this question edited Dec 20, 2017 at 4:40 Surjeet Bhadauriya asked Mar 3, 2016 at 13:02 Surjeet BhadauriyaSurjeet Bhadauriya 7,1563 gold badges38 silver badges54 bronze badges 6
  • 2 Why do you need html string in controller and not template this? – charlietfl Commented Mar 3, 2016 at 13:07
  • because i wanted to display a successfull message while user click on Save Button and i want that message look good. – Surjeet Bhadauriya Commented Mar 3, 2016 at 13:14
  • So make it look good in the template and pass in text...that is the normal approach. – charlietfl Commented Mar 3, 2016 at 13:17
  • don't i use in controller. If i want? – Surjeet Bhadauriya Commented Mar 3, 2016 at 13:19
  • Makes no sense doing it in controller. no separation of concerns and requires more complicated process to inject it. That's what templates are for – charlietfl Commented Mar 3, 2016 at 13:20
 |  Show 1 more comment

3 Answers 3

Reset to default 14

You need to inject $sce service into your controller or Directive etc. and use $sce service like this:-

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

And bind this in your HTML page e.g;

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

You have to secure your content with the $sce service and then use the

ng-bind-html directive

docs here.


EDIT

you can find the usage of sce.trustAsHtml here.

<p ng-bind-html="message"></p>
发布评论

评论列表(0)

  1. 暂无评论