呈现javascript对象内的HTML标记

时间:2021-05-09 15:54:43

I have a javascript object i am displaying in an angularjs web app. The object is a string characters with html tags "<h1></h1>" and "<p></p>" The problem is that when i render it on the html page is suppose to render the tags as regular html tags but it does not. it rather shows the tags raw character.

我有一个javascript对象,我在angularjs网络应用程序中显示。该对象是带有html标签“

”和“

”的字符串字符。问题是当我在html页面上呈现它时,假设将标签呈现为常规html标签但事实并非如此。它显示标签原始字符。

This is what is show on the html page

这是html页面上显示的内容

<h1>My Article title</h1><p>My article content goes here</p>

This is what i am expecting to see

这是我期待看到的

My Article Title

My Article content Goes here

我的文章内容在这里

Angular Code

          $scope.$apply(function(){                 
                $scope.eventsRaw = JSON.parse(data);
                $scope.eventDT = $scope.eventsRaw[0];
           })

HTML Code

<div>
  {{eventDT.name}}
  {{eventDT.desc}}
</div>

How do i make it render properly. the actual file are pulled from a database.

如何使其正确渲染。实际文件是从数据库中提取的。

Thank you.

1 个解决方案

#1


Use:

<span ng-bind-html="tagsString"></span>

https://docs.angularjs.org/api/ng/directive/ngBindHtml

Note: to use this you must include ngSanitize in your module dependencies.

注意:要使用此功能,必须在模块依赖项中包含ngSanitize。

In your code:

在你的代码中:

<div>
  <span ng-bind-html="eventDT.name"></span>
  <span ng-bind-html="eventDT.desc"></span>
</div>

#1


Use:

<span ng-bind-html="tagsString"></span>

https://docs.angularjs.org/api/ng/directive/ngBindHtml

Note: to use this you must include ngSanitize in your module dependencies.

注意:要使用此功能,必须在模块依赖项中包含ngSanitize。

In your code:

在你的代码中:

<div>
  <span ng-bind-html="eventDT.name"></span>
  <span ng-bind-html="eventDT.desc"></span>
</div>