【发布时间】:2014-07-10 10:30:13
【问题描述】:
我想从 JSON 对象的属性中呈现 HTML 标记并以模式表示它们。
我有这个 JSON 对象:
{
"fileUploadCategories": [
{
"id": "Bread",
"title": "GrandmaBread",
"info": "Some info text.",
"restService": "empty",
"detailedInfo": [ "<h1>Stedsnavn</h1>",
"",//line break
"<i>Some text on a new line.</i>",
"",//line break
"Some text on the next line again <b>Should be bold.</b>"]
},
{
"id": "Cake",
"title": "My Cake",
"info": "Info text blah.",
"restService": "empty",
"detailedInfo": "<b>Test</b> <br> This is a text on a new line <br> New line"
}
]
}
在我的 html 中,我使用 angularjs 和 ng-repeat 和 ng-bind-html 将数据绑定到引导模式。这是我的代码:
<div class="panel-group" id="accordion" ng-repeat="fileUpload in fileUploadConfig" >
<div class="panel panel-default" >
<div class="panel-heading">
<h3 class="panel-title">{{fileUpload.title}}</h3>
</div>
<div class="panel-body">
<p class="bg-info">{{fileUpload.info}}</p>
<div class="form-horizontal">
<button class="btn btn-primary" id="{{fileUpload.id}}HelpBox" style="float:right" data-toggle="modal" data-target="#{{fileUpload.id}}HelpModal">
Hjelp
</button>
</div>
</div>
</div>
<!-- Modal -->
<div id="{{fileUpload.id}}HelpModal"class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="myModalLabel">{{fileUpload.id}}Help</h4>
</div>
<div class="modal-body" ng-bind-html="fileUpload.detailedInfo">
{{fileUpload.detailedInfo.join('\n')}}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Lukk</button>
</div>
</div>
</div>
</div>
</div>
当我将我的 html 文本放在一个数组中时,它不会按预期呈现。 (第一个 JSON 对象)。它在第二个 JSON 对象中工作正常(只需删除 .join('\n') )。我在一个数组中表示 HTML 的原因是为了以后更容易更改文本并查看它的呈现方式,而不是将所有内容放在一个带有 html 标签的单行上,这看起来真的很乱。
有没有比使用数组来表示 JSON 对象中的数据并让它呈现 html 标签更好的方法?
【问题讨论】: