AngularJs:将json字符串转换为html表
我对angularjs来说绝对是个新手,在过去的三天里我一直在弄脏我的手。现在,需要将一个json字符串从rest端点转换为表格数据。这是我正在尝试的代码AngularJs:将json字符串转换为html表,html,angularjs,json,Html,Angularjs,Json,我对angularjs来说绝对是个新手,在过去的三天里我一直在弄脏我的手。现在,需要将一个json字符串从rest端点转换为表格数据。这是我正在尝试的代码 $scope.getDataCatalogue = function(){ $http.get('http://api.geosvc.com/rest/US/84606/nearby?apikey=485a35b6b9544134b70af52867292071&d=20&pt=PostalCode&
$scope.getDataCatalogue = function(){
$http.get('http://api.geosvc.com/rest/US/84606/nearby?apikey=485a35b6b9544134b70af52867292071&d=20&pt=PostalCode&format=json')
.success(function(data, status, headers, config){
//do something with your response
$scope = data;
})
.error(function(error){
console.log("not world");
});
}
$scope.getDataCatalogue = function(){
alert('getDataCatalogue');
}
现在,如何将json从数据转换为网格。这是解决问题的正确方法。或者在视图中自己动手:
<table>
<thead>
<tr>
<th>header 1</th>
<th>header 2</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="row in data">
<td>{{row.attribute1}}</td>
<td>{{row.attribute2}}</td>
</tr>
</tbody>
</table>
标题1
标题2
{{row.attribute1}}
{{row.attribute2}}
或者使用像ui grid这样的库来呈现更高级的表。如果数据中有固定的结构,那么您可以简单地使用
ng repeat
迭代对象(数据)并将其显示在预先创建的表上
示例如下所示:
考虑到这是您分配给$scope
变量名人员的对象<代码>$scope.people=数据代码>
[
{
id: 1,
firstName: "Peter",
lastName: "Jhons"
},
{
id: 2,
firstName: "David",
lastName: "Bowie"
}
]
在控制器中:
.success(function(data, status) {
$scope.people = data;
});
在HTML中:
<table>
<tr>
<th>Id</th>
<th>First Name</th>
<th>Last Name</th>
</tr>
<tr ng-repeat="person in people">
<td>{{person.id}}</td>
<td>{{person.firstName}}</td>
<td>{{person.lastName}}</td>
</tr>
</table>
身份证件
名字
姓
{{person.id}
{{person.firstName}
{{person.lastName}
您发现样板文件可以做到这一点的可能性非常小,如果您尝试在对数据进行排序后自己形成一个表,那么最好是您的数据是否以固定的已知结构出现?我的意思是,对象中的列数/属性数总是相同的吗?好的,检查我下面的答案,如果它足够描述性的话