Html 获取angular js中的嵌套JSON对象
我正在进行spa,希望从JSON获取文件,目前为止效果良好Html 获取angular js中的嵌套JSON对象,html,json,angularjs,nested,Html,Json,Angularjs,Nested,我正在进行spa,希望从JSON获取文件,目前为止效果良好 var app = angular.module("MyApp", []); app.controller("TodoCtrl", function($scope, $http) { $http.get('todos.json'). success(function(data, status, headers, config) { $scope.posts = data; }). error(fun
var app = angular.module("MyApp", []);
app.controller("TodoCtrl", function($scope, $http) {
$http.get('todos.json').
success(function(data, status, headers, config) {
$scope.posts = data;
}).
error(function(data, status, headers, config) {
alert("Error");
});
});
并将其绑定以重复列表
<ul ng-repeat="post in posts">
<li>
<b>Name : </b> {{post.name}} <br>
<b>Adress/street :</b> {{post.address.street}}
</li>
</ul>
我的尝试
post.address.street
无效。有什么建议吗?因为地址
是一个数组,所以您有两个选项
第一种选择:
使用数组表示法仅访问数组中的一个项。例如:
<ul ng-repeat="post in posts">
<li>
<b>Name : </b> {{post.name}} <br>
<b>Adress/street :</b> {{post.address[0].street}}
</li>
</ul>
您需要为Address对象使用另一个ng repeat。有关更多信息,请参阅。
<ul ng-repeat="post in posts">
<li>
<b>Name : </b> {{post.name}} <br>
<b>Adress/street :</b> {{post.address[0].street}}
</li>
</ul>
<ul ng-repeat="post in posts">
<li>
<b>Name : </b> {{post.name}} <br>
<div ng-repeat="address in post.address">
<b>Adress/street :</b> {{address.street}}
</div>
</li>
</ul>