简体   繁体   English

无法从响应JSON中删除html标记

[英]Unable to remove html tags from response JSON

Hi I am new to AngularJS. 嗨,我是AngularJS的新手。 I am having a problem parsing JSON data to proper format. 我在解析JSON数据到正确格式时遇到问题。 Actually the JSON response itself returned HTML format data (it contains HTML tags like &lt,;BR,> etc). 实际上,JSON响应本身返回了HTML格式数据(它包含HTML标签,如&lt ;; BR,>等)。 If I check the response in browser it returns fine, but in device(TAB,MOBILE) the HTML tags are also getting appended. 如果我在浏览器中检查响应它返回正常,但在设备(TAB,MOBILE)中,HTML标签也会被追加。 I am using AngularJS to bind the JSON response to DOM. 我使用AngularJS将JSON响应绑定到DOM。 Is there any way to simply ignore HTML tags in JQuery or in AngularJs? 有没有办法简单地忽略JQuery或AngularJs中的HTML标签? At the same time I don't want to remove the HTML tags as they are necessary to define "new line", "space", "table tag" etc. 同时我不想删除HTML标签,因为它们是定义“新行”,“空格”,“表格标签”等所必需的。

A sample response I am getting is like: 我得到的示例回复如下:

A heavier weight, stretchy, wrinkle resistant fabric.<BR><BR>Fabric Content:<BR>100% Polyester<BR><BR>Wash Care:<BR> 

If I apply the binding using {{pdp.desc}} , the HTML tags are also getting added. 如果我使用{{pdp.desc}}应用绑定,HTML标记也会被添加。 Is there any way to accomplish this? 有没有办法实现这个目标?

I have added ng-bind-html-unsafe="pdp.desc", but still "BR" tags r coming. 我添加了ng-bind-html-unsafe =“pdp.desc”,但仍然是“BR”标签。

可以使用regix表达式删除无用的html标签,试试这个

str.replace(/<\/?[^>]+>/gi, '')

尝试使用三对括号{{{pdp.desc}}}在Handlebars中它可以工作,可能在你的情况下。

Use JS HTML parser 使用JS HTML解析器

var pattern = @"<(img|a)[^>]*>(?<content>[^<]*)<";
var regex = new Regex(pattern);
var m = regex.Match(sSummary);
if ( m.Success ) { 
  sResult = m.Groups["content"].Value;

courtesy stackoverflow. 礼貌stackoverflow。

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

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