简体   繁体   English

使用angularjs与Json文件进行NG重复?

[英]Ng-repeat with a Json file using angularjs?

I have json file called brands.json : 我有一个名为brands.json的 json文件:

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

Im trying to iterate through this list and list the brand(eg Honda and Porsche) and render using HTML lists. 我试图遍历此列表并列出品牌(例如本田和保时捷),并使用HTML列表进行渲染。

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

JS: JS:

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

This code works fine but it displays the arrays inside the brand names eg instead if displaying Honda it displays ["Accord", "Civic"] . 这段代码可以正常工作,但是可以显示品牌名称中的数组,例如,如果显示本田,则显示[“ Accord”,“ Civic”] I want it to display the brand names only. 我希望它仅显示品牌名称。

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

Try: 尝试:

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

Quouted from the 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}. 例如:{'adam':10,'amalie':12}中的(name,age)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM