从JSON解析数据
我对包含缩略图和网站URL的JSON文件进行AJAX调用,然后使用AngularJS指令ng repeat to list列出每个文件。问题是缩略图和网站URL不再填充我的页面。我将JSON文件数据重新格式化为数组,因为我将向JSON文件添加多个数组,这些数组将包含不同的对象,用于整个网站的其他页面 JSON: 角度:从JSON解析数据,json,angularjs,ng-repeat,ngroute,Json,Angularjs,Ng Repeat,Ngroute,我对包含缩略图和网站URL的JSON文件进行AJAX调用,然后使用AngularJS指令ng repeat to list列出每个文件。问题是缩略图和网站URL不再填充我的页面。我将JSON文件数据重新格式化为数组,因为我将向JSON文件添加多个数组,这些数组将包含不同的对象,用于整个网站的其他页面 JSON: 角度: angular.module('myApp') .constant("dataUrl", "../json/data.json") .controller("websitesCo
angular.module('myApp')
.constant("dataUrl", "../json/data.json")
.controller("websitesController", function($scope, $http, dataUrl){
$scope.data ={};
$http.get(dataUrl)
.success(function(data){
$scope.data.projects = data;
})
.error(function(error){
$scope.data.error = error;
});
});
HTML:
<ul ng-controller="websitesController">
<li ng-repeat="item in data.projects">
<img ng-src="{{item.thumbnail}}" />
<div>
<a ng-href="{{item.website}}" target="_blank"><b>Website</b></a>
</div>
</li>
<ul>
-
应该是$scope.data.projects=data.websites 您的JSON数据有一个名为websites
的键,用于存储网站数组。您正在使用网站
键作为$scope.data.projects添加该对象。要访问网站数组,您需要查看该对象,即,您应该将$scope.data.projects
分配给data.websites
按如下方式更改成功处理程序:
编辑:数据示例--
您的响应数据
如下所示:
{"websites":[
{
"thumbnail": "thumbnail1.jpg",
"website": "http://somewebsite.com"
},
{
"thumbnail": "thumbnail2.jpg",
"website": "http://somewebsite.com"
},
{
"thumbnail": "thumbnail3.jpg",
"website": "http://somewebsite.com"
}
]}
但是,为了在ng repeat中使用它,您希望它看起来像这样(也经过解析):
[
{
"thumbnail": "thumbnail1.jpg",
"website": "http://somewebsite.com"
},
{
"thumbnail": "thumbnail2.jpg",
"website": "http://somewebsite.com"
},
{
"thumbnail": "thumbnail3.jpg",
"website": "http://somewebsite.com"
}
]
您还可以使用json过滤器{{item | json}}
记录该值,以查看实际迭代的内容!谢谢当我向JSON文件添加另一个键时,我可以使用该数组$scope.data.[something]=data.[key]@用户3574939您可以向JSON对象添加新的键值对,并按您所说的方式访问它们,但语法是$scope.data.newKey=data.newKey
或$scope.data['newKey']=data['newKey']
。您将“点表示法”和“括号表示法”语法混合在一起,这样会产生错误。如果有帮助的话,请给我的答案一点爱!
{"websites":[
{
"thumbnail": "thumbnail1.jpg",
"website": "http://somewebsite.com"
},
{
"thumbnail": "thumbnail2.jpg",
"website": "http://somewebsite.com"
},
{
"thumbnail": "thumbnail3.jpg",
"website": "http://somewebsite.com"
}
]}
[
{
"thumbnail": "thumbnail1.jpg",
"website": "http://somewebsite.com"
},
{
"thumbnail": "thumbnail2.jpg",
"website": "http://somewebsite.com"
},
{
"thumbnail": "thumbnail3.jpg",
"website": "http://somewebsite.com"
}
]