使用angularjs使用Json文件进行NG重复? [英] Ng-repeat with a Json file using angularjs?
问题描述
我有一个名为 brands.json 的json文件:
I have json file called brands.json:
{
"brands": {
"Honda": [
"Accord",
"Civic"
],
"Porsche": [
"Cayenne",
"Cayman"
]
}
}
我试图遍历此列表并列出品牌(例如本田和保时捷),并使用HTML列表进行渲染.
Im trying to iterate through this list and list the brand(e.g. Honda and Porsche) and render using HTML lists.
<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"] .我希望它仅显示品牌名称.
This code works fine but it displays the arrays inside the brand names e.g. instead if displaying Honda it displays ["Accord", "Civic"]. I want it to display the brand names only.
<li>Honda</li>
<li>Porsche</li>
推荐答案
尝试:
<li ng-repeat="(key, value) in items">{{key}}</li>
从文档中退出:
表达式中的(键,值)–其中键和值可以是任何用户 定义的标识符,而expression是作用域表达式,它给出了 枚举的集合.
(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.
例如:{'adam':10,'amalie':12}中的(姓名,年龄).
For example: (name, age) in {'adam':10, 'amalie':12}.
这篇关于使用angularjs使用Json文件进行NG重复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!