保存(转义?)html 并在 json 对象中使用它
Saving (escaped?) html and using it in a json object
我正在尝试将div对象的内部保存为json对象中的某些html,将其保存,然后将其作为json接收回,并让html在放置在div中时能够用作普通html。
所以这是我的尝试
$scope.contentHere = [
{
'size' : '',
'title' : 'Utility Alerts',
'content' : '<div>test 1</div>',
'help' : 'Help Text Here',
'launch' : true,
'share' : true,
'mobile' : true
},
{
'size' : 'w3',
'title' : 'Graph',
'content' : '<nvd3-stacked-area-chart data="exampleData" id="exampleId" showXAxis="true" showYAxis="true" showControls="true" width="700" height="200"> <svg></svg> </nvd3-stacked-area-chart>',
'help' : 'Help Text Here',
'launch' : true,
'share' : true,
'mobile' : true
}];
我正常尝试了一个,一个完全转义,两个似乎都只是在页面上呈现为正常文本。我重复了它们,并试图像这样显示 html -
<div class="item gs-w" ng-class="widget.size" ng-repeat="widget in contentHere" >
<div class="handle"><h5 ng-style="{ 'color' : themeHere.h1 }>{{widget.title}}</h5></div>
{{widget.content}}
</div>
将不胜感激任何帮助!感谢您的阅读。
你必须告诉 Angular 将内容绑定为 HTML,如下所示:
<div class="item gs-w" ng-class="widget.size" ng-repeat="widget in contentHere" >
<div class="handle"><h5 ng-style="{ 'color' : themeHere.h1 }>{{widget.title}}</h5></div>
<div ng-bind-html="widget.content"></div>
</div>
但是,从 1.2 开始,Angular 附带了启用$sce,这意味着您必须明确告诉 Angular 您信任该 HTML,如下所示:
// You will have to inject $sce in your controller.
$scope.contentHere = $scope.contentHere.map(function(entry){
entry.content = $sce.trustAsHtml(entry.content);
return entry;
});
另外,不确定这是否只是一个复制粘贴问题,但是在数组中的第二个对象中,内容以HTML实体编码,您将需要适当的,未编码的HTML。
您应该使用唯一标识符将 html 推送到$templateCache中,然后使用 ng-include 从视图中包含该模板。
或者,您可以创建一个自定义指令,该指令加载html并将其作为链接函数的一部分绑定到指令。
相关文章:
- jQuery匹配JSON对象的部分文本
- 如何在Javascript中将JSon对象转换为数组
- 我可以在json对象中添加一个函数吗
- 使用JS将数组转换为json对象
- 我应该如何将响应数据保存在对象(json)中以获得更好的操作和性能
- 用javascript从列表对象(JSON的)构建diffrent选项卡
- php请求带有多个对象json-jquery
- AngularJS,过滤器:如何将一个巨大的对象(JSON)变成一个数组
- 动态属性对象 - JSON
- JavaScript 对象 (JSON) 中的动态设置值
- 从数组查询获取对象 JSON 值时出错
- 使用jQuerygetJSON将多维对象JSON转换为HTML
- 需要从 Angular JS 应用程序中的另一个 JSON 对象数组填充 JSON 数组的每个对象 JSON 对象数组
- 嵌套对象JSON排序JavaScript
- PHP在MySQL中保存来自Javascript的对象"JSON.stringify"通过Ajax
- 将JavaScript对象/ JSON转换为PHP数组
- JSON对象.JSON对象内部
- 访问对象的对象- JSON API
- 数组到对象JSON
- Message":"传入的无效对象(JSON数据格式化问题)