gpt4 book ai didi

javascript - 关于如何在 Angular JS 中从 JSON 访问子对象的建议

转载 作者:行者123 更新时间:2023-11-28 15:19:56 25 4
gpt4 key购买 nike

我已经在 Angular 中创建了以下 Controller ,并且我正在尝试访问返回的 JSON 中的一些子对象。

Controller :

var app = angular.module("MyApp", []);

app.controller("FAQCtrl", function($scope, $http) {
$scope.dataLoaded = false;
$http.get('config/data.json', {
})
.success(function(data, status, headers, config) {
$scope.config = data;
$scope.dataLoaded = true;
}).
error(function(data, status, headers, config) {
window.alert("Sorry! There has been a problem!\n\nPANIC!")
});
});

返回的 JSON 采用以下格式:

{
"entryList":
{
"totalEntries": 237,
"incEntries": 10,
"offset": 0,
"loggingURL": "/action/logStatistics?action1=%27client%d%27",
"entry":
[
{
"catID": "delivery",
"entryID": "delivery-charges",
"subcatID": "delivery-options",
"question": "What are your delivery charges?"
},
{
"catID": "returns-and-refunds",
"entryID": "online-returns-policy",
"subcatID": "returns-policy",
"question": "What is your online returns policy?"
},
{
"catID": "delivery",
"entryID": "track-delivery",
"subcatID": "order-status",
"question": "How can I track my delivery/order?"
},
{
"catID": "delivery",
"entryID": "click-and-collect",
"subcatID": "delivery-options",
"question": "Do you offer Click and Collect or Collect +?"
},
{
"catID": "products-and-services",
"entryID": "makeover",
"subcatID": "services",
"question": "Can I come in for a free makeover and skin consultation?"
},
{
"catID": "delivery",
"entryID": "how-long-for-delivery",
"subcatID": "delivery-queries",
"question": "How long will it take for my products to be delivered?"
}
]
}

}

我希望能够返回提要的每个“条目”部分的详细信息,但我正在努力完成以下工作:

<div ng-repeat="item in entryList">
<a href="" class="btn btn-info btn-lg btn-block">{{item.entryID}}</a>
<br />
</div>

有人可以告诉我如何轻松访问子部分吗?

谢谢

最佳答案

这里似乎有几个问题。

  1. 您正在引用ng-repeat中的entryList,但您的作用域中没有名为entryList的变量。返回数据后,您需要在成功部分分配 $scope.entryList = data.entryList.entry;
  2. 如果这是加载时页面上的指令所需的数据,那么您应该将其传递到 Controller 中,而不是在 Controller 加载后填充它。这可以通过使用解析来完成。这是更多相关信息 https://thinkster.io/egghead/resolve

关于javascript - 关于如何在 Angular JS 中从 JSON 访问子对象的建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31942407/

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