我有点奇怪,因为角度不解析 JSON 中提供的 HTML。我正在使用 ng-bind-html-unsafe 指令,该指令从 JSON 中获取字符串并将其输出到页面上,但浏览器没有解析 html。
我正在使用 angularjs V1.1.5,但也尝试过旧版本。
我的绑定标签如下所示:
<div class="announcement-wrapper" ng-repeat="announcement in announcements" id="{{announcement.announcementId}}">
<p ng-bind-html-unsafe="announcement.content"></p>
</div>
提供绑定的 JSON 是:
{
"userId": "s123456",
"responseCode": "OK",
"responseMessage": "",
"timeStamp": 1371778685560,
"announcements": [
{
"announcementId": 1518,
"title": "Daves second announcement",
"content": "<p>Announcement for Chad</p>",
"releaseDate": "13 June 2013",
"important": false,
"read": true
},
{
"announcementId": 1511,
"title": "Onshore HE and TAFE Announcement",
"content": "<p>This announcement is only for Onshore HE and TAFE persons onshore.</p><p>High Priority.</p>",
"releaseDate": "04 June 2013",
"important": false,
"read": true
}
]
}
发生的事情是编码字符正在生成到页面上,只是浏览器没有解析它们以输出为实际的段落标签。例如。第二个公告正在输出到页面上
<p>This announcement is only for Onshore HE and TAFE persons onshore.</p><p>High Priority.</p>
我也尝试过使用 ng-bind-html 指令并设置了 sanitize 依赖项,但它也无法使用。毫无疑问,它很小,但我只需要一双新鲜的眼睛来看看......
提前致谢!