问题
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>
Ex. JSON:
{
title: '’ This is a title',
thumbnail: 'test.jpg',
published: 'October 15'
summary: '“ '
}
Ex. Output:
’ This is a title
October 15
“
回答1:
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>
Script
Polymer('your-polymer-element-name', {
encodeEntities: function(value) {
var div = document.createElement('div');
div.innerHTML = value;
return div.innerHTML;
}
});
Sources:
Polymer - Expressions: Custom Filters
Stackoverflow - How to assign HTML entities in Polymer element definition?
来源:https://stackoverflow.com/questions/26385986/converting-html-entities-from-json-to-string