Javascript 如何填充下拉列表,从观察者返回值?
我从服务中订阅数据,如下所示:Javascript 如何填充下拉列表,从观察者返回值?,javascript,html,angular,Javascript,Html,Angular,我从服务中订阅数据,如下所示: this.infoService.getName() .subscribe((data)=>{ console.log(data.json()); this.names.push(data.json()); }); 并将其填充到下拉列表中,如下所示: <select id='features' name='features'> <option *ng
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>
{{item}}
我的问题是整个响应出现在第0位。因此,整个响应出现在下拉列表中,而不是下拉列表中。有人能说我做错了什么吗?假设
数据
是数组类型
this.infoService.getName()
.subscribe((data)=>{
console.log(data.json());
this.names = data; // simply assign data array to this.names array
});
如果
数据
是字符串数组的意思[“a”、“b”、“c”]
,它将输出名称,而不会在html中进行任何更改
如果Data
是一个对象数组[{name:“a”},{name:“b”}]
,请按如下所示更改html
<option *ngFor="let item of names">
{{item.name}} // check this line
</option>
{{item.name}}//检查此行
我的问题是,整个回答都排在第0位。
-你能澄清一下吗?console.log(data.json())的输出是什么代码>?数据的响应是什么?它是一个对象还是一个数组?@Tommy输出是一个数组,但我的问题是整个数组都出现在dropdownlist上,而应该以下拉方式一个接一个地出现。因此,我得到了一个完整的宽下拉列表list@hbamithkumara如果我推送(数据)按钮上的输出是response ok 200……。@micronyks其A arraydata的类型为字符串array0:response{u body:[“A”、“B”、“C”、“D”]”,状态:200,ok:true,statusText:“ok”,标题:标题,}长度:1数据看起来非常奇怪。打印时不带.json()
并将其放在此处。不带json时,它必须是this.names=data[0]。\u body
但在某些地方仍然存在问题。