gpt4 book ai didi

javascript - 使用 Angular.js 解析 JSON 中的嵌套对象数组

转载 作者:行者123 更新时间:2023-12-02 16:18:27 25 4
gpt4 key购买 nike

我是 Angular.js 新手。在这里,我正在开发一个购物车,其中必须显示产品的图像、名称和成本。我有多个租户,每个租户都有数组 listOfBinaries,其中包含 listOfMerchandise。我的问题是我必须显示租户的姓名、listOfBinary 的图像和 listOfMerchandise 的成本。我解析了 JSON,如下所示,但在运行时我得到了空页面。谁能帮帮我吗。这是我对 REST URL 进行 AJAX 调用后得到的 JSON:

{
"_links": {
"search": {
"href": "http://localhost:8080/sportsrest/tenants/search"
}
},
"_embedded": {
"tenants": [
{
"name": "tenant1",
"domainName": "gaming.com",
"description": "sasasa",
"listOfBinary": [
{
"imageURL": "http://i.telegraph.co.uk/multimedia/archive/02602/premier-league_2602603b.jpg",
"username": "Sumanth",
"description": "imagSky Sports offer free live Premier League action on o Sky Sports offer free live Premier League action on opening weekend of season as battle witpening weekend of season as battle wite1",
"listOfMerchandise": [
{
"rate": 100,
"type": "item1",
"shortDescription": "test1"
}
]
},
{
"imageURL": "http://images.clipartpanda.com/sports-equipment-clipart-black-and-white-soccer-ball-hi.png",
"username": "as",
"description": "Sky Sports offer free live Premier League action on opening weekend of season as battle wit Sky Sports offer free live Premier League action on opening weekend of season as battle wit",
"listOfMerchandise": [
{
"rate": 200,
"type": "item2",
"shortDescription": "test2"
}
]
}
],
"merchandise": null,
"_links": {
"self": {
"href": "http://localhost:8080/sportsrest/tenants/2"
},
"listOfCustomerPackage": {
"href": "http://localhost:8080/sportsrest/tenants/2/listOfCustomerPackage"
}
}
},
{
"name": "tenant2",
"domainName": "cric.io",
"listOfBinary": [
{
"imageURL": "http://i.telegraph.co.uk/multimedia/archive/02602/premier-league_2602603b.jpg",
"username": "Sumanth",
"description": "Sky Sports offer free live Premier League action on opening weekend of season as battle wit Sky Sports offer free live Premier League action on opening weekend of season as battle wit",
"listOfMerchandise": [
{
"rate": 500,
"type": "item5",
"shortDescription": "test5"
}

]
},
{
"imageURL": "www.test.com",
"username": "sumanth",
"description": "sample",
"listOfMerchandise": [
{
"rate": 2323,
"type": "type7",
"shortDescription": "test"
}
]
}
],
"merchandise": null,
"_links": {
"self": {
"href": "http://localhost:8080/sportsrest/tenants/9"
},
"listOfCustomerPackage": {
"href": "http://localhost:8080/sportsrest/tenants/9/listOfCustomerPackage"
}
}
},
{
"name": "tenant5",
"domainName": "test.co",
"description": "test 123 ",
"listOfBinary": [],
"binary": {
"fileLocation": "www.test.com",
"username": "sumanth",
"description": "sample",
"listOfMerchandise": [
{
"rate": 2323,
"type": "trt",
"shortDescription": "rtrtrt"
}
]
}
}
]
}

}

我的directives.js 文件:当我进行此 Ajax 调用时,我得到的结果高于 JSON

 myAppDirectives.
controller('items_display', function ($scope,$http) {
$http({ method: 'GET', url: 'http://localhost:8080/sportsrest/tenants/' }).success(function (data) {
$scope.carts=data;
}).
error(function (data) {
alert("error" + data);
console.log(data);
});
});

我的 HTML 页面:

<!DOCTYPE html>
< html ng-app="myApp">
<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>

</head>
<body>
<div ng-app="myApp" >
<div ng-controller="items_display">
<div ng-repeat="item in carts._embedded.tenants">
<div type="text" class="item_name" value={{item.name}} >
<div ng-repeat="item in carts._embedded.tenants.listOfBinary">
<img class="shop_img" ng-src="{{item.fileLocation}}" ng-style="{'width':'100px', 'height':'100px'}" />
<div ng-repeat="item in carts._embedded.tenants.listOfBinary.listOfMerchandise">
<div type="text" class="item_cost" value={{item.rate}} >
</div>
</br>
</div>
</div>
</div>
</body>

谁能帮忙使用 Angular.js 在 html 页面上显示产品详细信息。

提前致谢

最佳答案

你的标记全乱了,检查我用你的数据和标记制作的plunker

http://plnkr.co/edit/jGCm6nO9S4hlNxLCyrSy?p=preview

<body>
<div ng-app="myApp">
<div ng-controller="items_display">
<div ng-repeat="tenant in carts._embedded.tenants">
<div type="text" class="item_name">{{tenant.name}}
<div ng-repeat="binary in tenant.listOfBinary">
<img class="shop_img" ng-src="{{binary.fileLocation}}" ng-style="{'width':'100px', 'height':'100px'}" />
<div ng-repeat="(key, value) in binary.listOfMerchandise[0]">
<div type="text" class="item_cost">{{key}}: {{value}}
</div>
<br>
</div>
</div>
</div>
</div>
</div>
</div>
</body>

关于javascript - 使用 Angular.js 解析 JSON 中的嵌套对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29365789/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com