本文介绍了将HTML实体从JSON转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用聚合物,但对它是新手(今天才开始使用它),在显示来自JSON的数据时遇到了一些麻烦,例如
,’
,“
等.
I'm using polymer and I'm new to it (just started using it today) and I have some troubles displaying data coming from JSON like
, ’
, “
etc.
HTML
<news-card>
<h1>{{summary.title}}</h1>
<img src="{{summary.thumbnail}}"></img>
<span>{{summary.published}}</span>
<p>{{summary.summary}}</p>
</news-card>
例如JSON:
{
title: '’ This is a title',
thumbnail: 'test.jpg',
published: 'October 15'
summary: '“ '
}
例如输出:
’ This is a title
October 15
“
推荐答案
我使用自定义过滤器来显示所需的输出.
I used custom filters to display the desired output.
HTML
<news-card>
<h1>{{summary.title | encodeEntities}}</h1>
<img src="{{summary.thumbnail}}"></img>
<span>{{summary.published}}</span>
<p>{{summary.summary | encodeEntities}}</p>
</news-card>
脚本
Polymer('your-polymer-element-name', {
encodeEntities: function(value) {
var div = document.createElement('div');
div.innerHTML = value;
return div.innerHTML;
}
});
来源:
Stackoverflow-如何在Polymer元素定义中分配HTML实体?
这篇关于将HTML实体从JSON转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!