Javascript 保存(转义?)html并在json对象中使用
我试图将div对象的内部保存为json对象中的一些html,保存它,然后将其作为json接收回来,并使html在放置到div中时能够作为普通html使用 这就是我的尝试Javascript 保存(转义?)html并在json对象中使用,javascript,html,json,angularjs,escaping,Javascript,Html,Json,Angularjs,Escaping,我试图将div对象的内部保存为json对象中的一些html,保存它,然后将其作为json接收回来,并使html在放置到div中时能够作为普通html使用 这就是我的尝试 $scope.contentHere = [ { 'size' : '', 'title' : 'Utility Alerts', 'content' : '<div>test 1</div>', 'help' : 'Help Text Here', 'launch' : true,
$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
}];
$scope.contentHere=[
{
“大小”:“,
“标题”:“实用程序警报”,
“内容”:“测试1”,
“帮助”:“此处的帮助文本”,
"发射":对,,
"分享":对,,
“手机”:真的吗
},
{
“大小”:“w3”,
“标题”:“图表”,
“内容”:“nvd3堆叠面积图数据=“exampleData”id=“exampleId”showXAxis=“true”showYAxis=“true”showControls=“true”width=“700”height=“200”svg/svg/nvd3堆叠面积图”,
“帮助”:“此处的帮助文本”,
"发射":对,,
"分享":对,,
“手机”:真的吗
}];
我尝试了一个正常的,一个完全转义的,两个似乎都只是呈现为页面上的正常文本。
我把它们放在一个重复中,并试图像这样显示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>
// 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并将其作为链接函数的一部分绑定到该指令