用Json数据封装Html
Wrapping Html with Json Data
我遇到的问题是执行后,页面不会得到json数据,页面会以字符串的形式显示以下内容。但是我所有的web api服务都成功地获取了数据。
{{recipe.Name}}
{{recipe.desc}}
{{recipe.allergen}}
{{recipe.cost}}
我的HTML:
<accordion close-others="false" >
<div>
<accordion-group class="div-recipe-header">
<accordion-heading></accordion-heading>
<div class="inner"></div>
</accordion-group>
</div>
</accordion>
我的JavaScript包装
<script>
$(document).ready(function () {
aa = global.userhtml// handles the UserHtml Data in Json return
$(".inner").wrapAll(aa)
})
</script>
示例JSON数据:
{
ID: "0908",
UserHtml: "<table ><tbody><trclass="recipe-list" data-ng-repeat="recipe in
recipe_data" ng-if="recipe.Keyword == keyw.Keyword" ><td><center>
<span id="img{{recipe.ID}}X"><a id="{{recipe.ID}}" href="javascript:void(0);">
<img ng-if="recipe.ID !=='" fallback-src="images/default.png" ng-src="
{{recipe.Pictures}}" class="images" id="img{{recipe.ID}}"/> <div class="div-recipe-
allergen">{{recipe.allergen}}</div><div class="div-recipe-cost">{{recipe.cost}}
</div></td></tr></tbody></table>"
}
工作Plunker:
http://plnkr.co/edit/KOqwCFTD4aeaCsumRdom?p=preview
// DISCLAIMER: This is EXTREMELY DIRTY, it "works" but have many potential issues
// (depending what you are trying to do with it which has not been stated at all in the question)
var myglobal = {
userhtml: '<table><tbody><tr class="recipe-list" ng-repeat="recipe in recipe_data" ng-if="recipe.Keyword == keyw.Keyword" ><td><center><span id="img{{recipe.ID}}X"><a id="{{recipe.ID}}" href="javascript:void(0);">'+
'<img ng-if="recipe.ID !==''''" fallback-src="images/default.png" ng-src="{{recipe.Pictures}}" class="images" id="img{{recipe.ID}}"/> <div class="div-recipeallergen">{{recipe.allergen}}</div><div class="div-recipe-cost">{{recipe.cost}}</div></td></tr></tbody></table>',
recipe_data: [ { ID: '1234', Pictures:'/picture.jpeg',
Keyword: 'food', allergen:'peanuts', cost:'5$' },
{ ID: '1235', Pictures:'/picture.jpeg',
Keyword: 'food', allergen:'kiwi', cost:'10$' } ]
};
var app = angular.module('plunker', []);
app.directive("myUserHtml", ['$compile', function($compile){
return {
restrict: 'C',
link: function(scope, element, attrs) {
// Extremely dirty hack!
scope.myglobal = myglobal;
scope.recipe_data = myglobal.recipe_data;
scope.keyw = {Keyword:"food"};
scope.$watch('myglobal.userhtml', function() {
element.html(scope.myglobal.userhtml);
$compile(element.contents())(scope);
});
}
};
}]);
HTML已经被修改(注意myuserhtml类:这是触发上面定义的指令的类(与名称myUserHtml
匹配)。
<accordion close-others="false">
<div>
<accordion-group class="div-recipe-header">
<accordion-heading></accordion-heading>
<div class="inner my-user-html"></div>
</accordion-group>
</div>
</accordion>
我把"数据ng重复"改为"ng重复"。我怀疑您的服务器端代码没有正确处理一行中的两个单引号(并将其转换为一个单引号)。我添加了反斜杠,所以它读取recipe.ID !==''''
——这是正确的转义,所以最终结果HTML只是recipe.ID !==''
您在服务器上使用什么语言来生成JSON?(或者更好的是向我们展示源代码!!)它没有正确地为JSON转义字符串。使用工具或库来构建json文档如果您不了解发生了什么,请不要使用字符串串联。
@KeenEgs jasonSCript是对的,您的javascript字符串严重损坏。它不仅仅是"将单引号改为双引号"和将"双引号改为单引号"。Javascript和HTML支持单引号或双引号。如果你试图将一个字符串"嵌套"在另一个字符串中,你必须格外小心。你必须真正理解这个概念才能在编程中取得成功。
var a="abcd";//有效字符串-正常
var a="ab"cd";//这是无效的,中间的引号将结束离开cd的字符串";作为无效的javascript。
var a="ab''"cd";//这是有效的:"反斜杠"''用于表示下一个字符是字符串的一部分,而不是终止字符串。
var a='ab"cd';//这是有效的:使用不同的引号,这样就不会混淆。
由于您的模板包含字符串(我也在其中看到了一个引号),您必须格外小心。您甚至可能最终拥有3个级别:字符串内部字符串内部字符串!
您的JSON字符串无效。您正在提前结束字符串报价
{
ID: "0908",
UserHtml: "<table ><tbody><trclass="..... <- You're closing your quotes here
}
尝试使用单引号代替
{
ID: "0908",
UserHtml: '<table ><tbody><trclass="..etc..."
| ^ double quotes for html within JSON string
^ single quotes around JSON object
}
还要确保没有包含任何新行。
我建议检查JSON lint站点,如http://jsonlint.com/验证您的JSON
I关于json字符串格式,其他答案可能是正确的,但最大的障碍是,除非先编译,否则不能将传入的html字符串视为Angular的模板。在使用html段之前尝试这样做:
global.userhtml = $compile(global.userhtml)($scope);
- 在页面呈现之前更改HTML数据
- 当使用ajax并将html数据保存为对象时,收听浏览器返回按钮.好的或坏的
- 闭包如何具体化数据封装
- 使用数据属性将HTML数据复制到另一个元素
- 使用jQuery在select选项上设置HTML数据属性
- 使用Javascript而不是html数据属性配置Parsley
- 访问HTML数据集
- 如何调用更改事件,例如在 HTML 数据列表上选择
- 检测 点击 在 ANDROID 中使用 HTML 数据查看
- 选择具有特定值的 html 数据属性
- jQuery的HTML数据效果
- 生成html时,在html数据属性中存储由.push()填充的数组
- Powerbuilder/Javascript HTML数据窗口SetItem日期时间失败
- html数据属性在firefox中使用javascript时没有按预期显示,但在chrome中显示正确
- 由于数据中的 & 符号,无法在 C# 上传递整个 html 数据
- 内联 HTML 数据角色<>脚本 jquery:Kendo Widget Initialization
- 如何将带有普通对象集合的 Js 数组对象传递给 ajax post'html' 数据类型中的控制器
- 使用 PrototypeJS 1.6 访问锚点上的 HTML 数据属性
- 如何更改 HTML 数据列表输入上显示的图标?隐藏是可能的,但我可以将其更改为其他一些图标,例如向下箭头
- 用Json数据封装Html