简体   繁体   English

如何将API响应(json)绑定到Angular4中的下拉列表

[英]How to bind API response (json) to drop-down in Angular4

I am working in an angular4 application in this I need to bind a drop-down from API response data . 我正在这个angular4应用程序中工作,我需要从API响应数据绑定一个下拉列表。

I don't know how to fetch the specific data for the particular section from API. 我不知道如何从API获取特定部分的特定数据。

Here API contains Category ,Group and Sub-group data .Each Group has 2 sub-groups. API包含类别,组和子组数据。每个组有2个子组。

I have created a stackblitz file for this Please take a look 我为此创建了一个stackblitz文件,请看一看

https://stackblitz.com/edit/angular-bootstrap-carousel-dynamic2-y6iknh?file=app%2Fapp.component.ts https://stackblitz.com/edit/angular-bootstrap-carousel-dynamic2-y6iknh?file=app%2Fapp.component.ts

Here I receiving the API response ,But don't know how to assign the values to the dropdown positions. 在这里,我收到API响应,但是不知道如何将值分配给下拉位置。

Can anyone guide me to solve this .. 谁能指导我解决这个问题..

You need to bind data with some variable by parsing it into json() , then you will be able to fetch and consume data into your view like this 您需要通过将数据解析为json()来将数据与某些变量绑定,然后就可以像这样将数据获取并使用到视图中

 ngOnInit() {
    this.CartdataService.get_New_Products().subscribe(
      data => {
        this.dropdownData = data.json();
                  console.log(data.json());
      });


<div class="col-sm-12" style="margin-top: 20px !important" *ngFor='let data of dropdownData; let i=index'>
    <div class="col-sm-12 opt1">
        <div class="row">
            <h5>
                <a class="col-sm-12 font-weight-normal">
          {{data?.CAMD_ENTITY_DESC}}
        </a> 
            </h5>
            <i class="fa fa-angle-down ind" data-toggle="collapse" [attr.href]="'#collapseExample' + data?.CAMD_ENTITY_DESC"></i>
        </div>
    </div>
    <div class="col-sm-12 collapse opt1" [id]="'collapseExample' + data?.CAMD_ENTITY_DESC"
  *ngFor='let group of data?.group; let j=index'>
        <div class="row">
            <h6>
                <p class="dropdown-item col-sm-10">{{group?.CAMD_PRGRP_DESC}} </p>
            </h6>
            <i class="fa fa-angle-down ind arrow" data-toggle="collapse" [attr.href]="'#innerCollapse' + group?.CAMD_PRGRP_DESC"></i>
        </div>
    <div class="collapse col-sm-12" [id]="'innerCollapse' + group?.CAMD_PRGRP_DESC">
        <div class="row" *ngFor='let subgroup of group?.subgroup; let i=index'>
            <h6>
                <a class="dropdown-item opt">{{subgroup?.CAMD_PRSGRP_DESC}}</a>
            </h6>
        </div>
    </div>
    </div>
</div>

Here is working example for the same 这是相同的工作示例

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

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