简体   繁体   English

如何从JSON中读取数据并将其绑定到Angular 2+中的Select下拉列表

[英]How can I read and bind data from this JSON to a Select dropdown in Angular 2+

This is what my JSON looks like: 这是我的JSON的样子:

{
    "Stations": {
        "44": {
            "NAME": "Station 1",
            "BRANCH_CD": "3",
            "BRANCH": "Bay Branch"
        },
        "137": {
            "NAME": "Station 2",            
            "BRANCH_CD": "5",
            "BRANCH": "Beach Branch"        
        }
    }
}

I would like to know if its possible to extract the "NAME" and "BRANCH" values from each "Station" and bind them to separate Select inputs [dropdowns] 我想知道是否有可能从每个“站”中提取“名称”和“分支”值并将它们绑定到单独的选择输入[下拉列表]

I am using Angular 4. Here is what I have: 我正在使用Angular4。这是我所拥有的:

 this.apiService.getStations().subscribe(data => {
      this.stationList = data.Stations;
     }, err => {
      this.errorFetchingStations = err;
      console.log("Error getting list of stations " + err);
    })

this.stationList gives me an object of this format in the console: this.stationList在控制台中给了我这种格式的对象:

{44:{NAME: "Name of station", BRANCH: "Name of branch"}}
{137:{NAME: "Name of station", BRANCH: "Name of branch"}}

and so on. 等等。

If I try to bind the stationList to my select like this, I get an error 如果我尝试像这样将stationList绑定到我的选择,则会出现错误

ERROR Error: Error trying to diff '[object Object]'. 错误错误:尝试与'[object Object]'进行比较时出错。 Only arrays and iterables are allowed 仅允许数组和可迭代

    <select class="form-control">
        <option *ngFor="let station of stationList" [value]="station.NAME">{{station.NAME}}</option>
      </select>

You are having key value pair to the object instead ngFor expects an array so you should use the below code 您具有对象的键值对,而不是ngFor需要一个数组,因此应使用以下代码

 this.apiService.getStations().subscribe(data => {
      this.stationList = Object.values(data.Stations); // returns an array
     }, err => {
      this.errorFetchingStations = err;
      console.log("Error getting list of stations " + err);
    })

you should transform the stationList to array and you can use the Object.values(data.Stations), as the below. 您应该将stationList转换为数组,然后可以使用Object.values(data.Stations),如下所示。

this.apiService.getStations().subscribe(data => {
  var stations = data.Stations;
  this.stationList = Object.values(stations);
}, err => {
  this.errorFetchingStations = err;
  console.log("Error getting list of stations " + err);
})

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

相关问题 如何将数据从JSON响应绑定到下拉列表Angular2中? - How to bind data from JSON response into Dropdown List Angular2? 如何在Angular 2+中获取JSON数据 - How to fetch json data in Angular 2+ 如何将来自数据库的数据绑定到角度动态表单下拉列表中? - How to bind data that come from database into angular dynamic form dropdown? React JS - 如何将 json 数据绑定到下拉列表 - React JS - How do I bind json data to a dropdown list 从JSON数据中以角度填充多个下拉选择框 - populate multiple dropdown select boxes from JSON data in angular 如何绑定 Jquery Ajax Json 响应下拉列表? - How can I bind Jquery Ajax Json Reponses to a dropdown? 在将JSON数据绑定到HTML表Jquery的同时,从JSON绑定每个表列(标题)的选择下拉列表 - Bind select dropdown for each table column (Head) from JSON, while binding JSON data to HTML table Jquery Angular 2+和Observables:无法绑定到“ ngModel”,因为它不是“ select”的已知属性 - Angular 2+ and Observables: Can't bind to 'ngModel' since it isn't a known property of 'select' 我如何从ajax响应中正确读取json数据? - How can I properly read json data from ajax response? 如何正确读取 url 中的 JSON 数组数据 - how can i read JSON array data from url correctly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM