[英]How to get data from database to dropdown list in angular using REST API
I have created an API for getting all services, I want to put the name of services in dropdown list, I have tested this code but it doesn't work!我创建了一个 API 来获取所有服务,我想将服务的名称放在下拉列表中,我已经测试了这段代码,但它不起作用!
HTML HTML
<div class="col-lg-6">
<select name="Service"
class="form-control">
<option>-- Select Services --</option>
<option [value]="serv.service_name" *ngFor="let serv of ServiceList">
{{ serv.service_name}}
</option>
</select>
</div>
TS TS
ServiceList: any;
ngOnInit(): void {
this.serviceService.getAllServices().subscribe((data: any)=>{
this.ServiceList = data;
//console.log(this.services);
})
}
Since there is no error in your console, the only reason your dropdown list would be empty is that your data
is actually an empty array, ie []
.由于您的控制台中没有错误,因此您的下拉列表为空的唯一原因是您的data
实际上是一个空数组,即[]
。
Add console.log(data);
添加console.log(data);
and check what your API returns.并检查您的 API 返回的内容。
EDIT:编辑:
According to your comment, your API returns:根据您的评论,您的 API 返回:
[{…}] 0: {..., name_service: 'AAAA', ...} length: 1 [[Prototype]]: Array(0)
The property is named name_service
and NOT service_name
as you are using it in your HTML template...该属性被命名为name_service
而不是service_name
,因为您在 HTML 模板中使用它...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.