猿问

Angular 2:如何从 API 获取服务响应数据到 Select Option

api.services.ts


 listNama() {

     return this.http.get(this.BaseURL + 'welcome/getnama')

            .pipe(map(response => {

                 return response;

            }));

 }

我得到这样的回应


{

"status": "OK",

"Output": [

    {

        "id_pemborong": "1569079912",

        "nama": "ayub"

    },

    {

        "id_pemborong": "1569125109",

        "nama": "Hartono"

    },

    {

        "id_pemborong": "1569319859",

        "nama": "agus"

    },

    {

        "id_pemborong": "1569416787",

        "nama": "joko"

    }

   ]

 }

我如何将此响应绑定到角度选择选项。帮助


慕姐4208626
浏览 151回答 3
3回答

慕姐8265434

服务listNama() {&nbsp; &nbsp; return this.http.get(this.BaseURL + 'welcome/getnama')}然后在你的组件中订阅它/decalre a variableNameList:any[]=[];/ replace service with your servicenamethis.service.function.subscribe(arg => this.NameList = arg);并在模板中<select&nbsp; [(ngModel)]="selected" (change)="onChange($event)"><option [label] ="nama" *ngFor="let item of NameList" [selected]="selected"&nbsp;&nbsp; [value]="nama">{{nama}}</option></select>

森栏

Ts&nbsp; &nbsp; NameList:any[]=[];&nbsp; &nbsp; listNama() {&nbsp; &nbsp; return this.http.get(this.BaseURL + 'welcome/getnama')&nbsp; &nbsp; &nbsp; .pipe(map(response => {&nbsp; &nbsp; &nbsp; this.NameList= response.Output;&nbsp; &nbsp; &nbsp; }));&nbsp; &nbsp; &nbsp;}html&nbsp; &nbsp; <select&nbsp; [(ngModel)]="selected" (change)="onChange($event)">&nbsp; &nbsp; &nbsp; &nbsp;<option [label] ="nama" *ngFor="let item of NameList" [selected]="selected" [value]="nama">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {{nama}}&nbsp; &nbsp; &nbsp; &nbsp;</option>&nbsp; &nbsp; </select>
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答