簡體   English   中英

如何填充下拉列表,觀察者返回的值?

[英]How to populate the dropdown ,values being returned from observer?

我將服務中的數據訂閱為:

this.infoService.getName()
        .subscribe((data)=>{
            console.log(data.json());
            this.names.push(data.json());
        });

並將其填充到下拉列表中,如下所示:

<select  id='features' name='features'>
    <option *ngFor="let item of names">
      {{item}}
    </option> 
</select>

我的問題是整個響應都出現在第 0 個 position 中。結果整個響應都出現在下拉列表中而不是下拉列表中。有人能說我做錯了什么嗎?

假設data是數組類型。

this.infoService.getName()
        .subscribe((data)=>{
             console.log(data.json());
            
             this.names = data;   // simply assign data array to this.names array 
});

如果Data是字符串數組表示["a", "b", "c"] ,它將 output 名稱,html 沒有任何變化。

如果Data是對象數組[{name:"a"}, {name:"b"}] ,則將 html 更改如下,

<option *ngFor="let item of names">
      {{item.name}}                      // check this line
</option>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM