gpt4 book ai didi

javascript - 使用 angularjs 对 Json 文件进行 Ng 重复?

转载 作者:行者123 更新时间:2023-11-30 08:44:15 24 4
gpt4 key购买 nike

我有一个名为 brands.json 的 json 文件:

{
"brands": {
"Honda": [
"Accord",
"Civic"
],
"Porsche": [
"Cayenne",
"Cayman"
]
}
}

我尝试遍历此列表并列出品牌(例如本田和保时捷)并使用 HTML 列表呈现。

<li ng-repeat="brands in items">{{brands}}</li>

JS:

$scope.items= [];
$http.get('brands.json').then(function(response) {
$scope.items =response.data.brands;
});

这段代码工作正常,但它显示了品牌名称中的数组,例如相反,如果显示 Honda,它会显示 ["Accord", "Civic"]。我希望它只显示品牌名称。

<li>Honda</li>
<li>Porsche</li>

最佳答案

尝试:

<li ng-repeat="(key, value) in items">{{key}}</li>

引自docs :

(key, value) in expression – where key and value can be any user defined identifiers, and expression is the scope expression giving the collection to enumerate.

For example: (name, age) in {'adam':10, 'amalie':12}.

关于javascript - 使用 angularjs 对 Json 文件进行 Ng 重复?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23324100/

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