简体   繁体   English

如何使用angular显示HTML代码

[英]How to show HTML code using angular

In one of my application I am using angularjs for internalization. 在我的一个应用程序中,我正在使用angularjs进行内部化。 I am having different json files from which i am getting texts as per the market. 我有不同的json文件,根据市场从我那里获取文本。 I am importing them into XML. 我将它们导入XML。 Then in in jsp i am importing those XML to view. 然后在jsp中,我将导入那些XML以进行查看。

The problem is if i am giving some HTML tag inside XML like 问题是如果我在XML内提供一些HTML标记,例如

<sup>1</sup>

its working, but the same thing is not working if it passed from JSON. 它可以正常工作,但如果它是通过JSON传递的,则不会起作用。 Please help.. thanks in advance 请帮助..在此先感谢

json is
"pwpDetail": "<sup>1</sup>

Controller -  
$http.get(languageFilePath2).success(function(data){
     $scope.datav=data;
 });

XML - XML-

<description>{{datav.pwpDetail}}</description>

The print the HTML code as string not as a super script 将HTML代码打印为字符串而不是超级脚本

<sup>1</sup> You just need to Decode the output to get back the original HTML. <sup> 1 </ sup>您只需要解码输出以取回原始HTML。

Use javascript unescape function 使用javascript unescape函数

I think that you would want this: 我认为您会想要这样:

For Angular 1.3, use ng-bind-html in the HTML: 对于Angular 1.3,请在HTML中使用ng-bind-html:

<div ng-bind-html-unsafe="datav.pwpDetail"></div>

And in the controller: 并在控制器中:

function htmlDecode(input){
  var e = document.createElement('div');
  e.innerHTML = input;
  return e.childNodes.length === 0 ? "" : e.childNodes[0].nodeValue;
}  

$http.get(languageFilePath2).success(function(data){
     data.pwpDetail = htmlDecode(data.pwpDetail);
     $scope.datav=data;
 });

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

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