[英]“Not well-formed” firefox error while looping through JSON file with AngularJS
The JSON file structure is as follows: JSON文件结构如下:
{
"meta": {
"limit": 20,
"next": null,
"offset": 0,
"previous": null,
"total_count": 2
},
"objects": [
{
"description": "Lorem ipsum dolor sit amet ....Lorem ipsum dolor sit amet ....Lorem ipsum dolor sit amet ....",
"end": "2013-09-09T15:37:24",
"id": 1,
"name": "Lorem ipsum dolor sit amet ....Lorem ipsum dolor sit amet",
"resource_uri": "/api/task/1/",
"start": "2013-09-09T15:37:23"
},
{
"description": "Here goes description :) Here goes description :) Here goes description :) ",
"end": "2013-09-11T15:58:01",
"id": 2,
"name": "Some other task",
"resource_uri": "/api/task/2/",
"start": "2013-09-11T15:58:00"
}
]
}
I was trying to loop through it like so: 我试图像这样循环:
<!DOCTYPE html>
<html lang="en" ng-app id="ng-app">
<head>
<title>Angular studies</title>
<meta charset="UTF-8" />
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
<script>
function PostsCtrlAjax($scope, $http) {
$http({method: 'POST', url: 'posts.json'}).success(function(data) {
$scope.tasks = data;
});
}
</script>
</head>
<body>
<div id="ng-app" ng-app ng-controller="PostsCtrlAjax">
<div ng-repeat="task in tasks">
<a href='{{task.id}}'>{{task.name}}</a>
</div>
</div>
</body>
</html>
And for this I'm getting "Not well-formed" error in Firefox. 为此,我在Firefox中遇到“格式不正确”的错误。 Can you please show me a way to mend this? 你能告诉我一个修补这个的方法吗?
This is indeed valid JSON. 这确实是有效的JSON。 The problem is that you try to iterate over tasks
, but in fact want to iterate over tasks.objects
问题是你尝试迭代tasks
,但实际上想要迭代tasks.objects
for (var i=0;i<tasks.objects.length;i++) {
console.log(tasks.objects[i].id);
console.log(tasks.objects[i].name);
}
outputs 输出
1
Lorem ipsum dolor sit amet ....Lorem ipsum dolor sit amet
2
Some other task
I am not familiar with the angular syntax, but I guess this is what you want / really try to do : 我不熟悉角度语法,但我想这是你想要/真正尝试做的:
<div ng-repeat="task in tasks.objects">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.