![](/img/trans.png)
[英]How can I populate an array with selected values from dropdown in Angular?
[英]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.