-2

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!

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

ServiceList: any;
ngOnInit(): void {
this.serviceService.getAllServices().subscribe((data: any)=>{
  this.ServiceList = data;
  //console.log(this.services);
})  
}

It doesn't get data from database !! enter image description here

4
  • Is there any errors you can see in developer console (F12)? Commented Aug 7, 2022 at 15:15
  • No there isn't, it doesn't get data
    – Dev
    Commented Aug 7, 2022 at 15:23
  • can you show getAllServices function of serviceService service Commented Aug 7, 2022 at 15:25
  • getAllServices(){ return this.http.get<any>("127.0.0.1:8000/api/auth/getAllServices/"); }
    – Dev
    Commented Aug 7, 2022 at 15:33

1 Answer 1

2

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, i.e. [].

Add console.log(data); and check what your API returns.

EDIT:

According to your comment, your API returns:

[{…}] 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...

4
  • [{…}] 0: {id: 8, name_service: 'AAAA', desc_service: 'abcd', price_service: 120} length: 1 [[Prototype]]: Array(0)
    – Dev
    Commented Aug 7, 2022 at 15:55
  • @Dev I edited my answer. Please read the EDIT section. Commented Aug 7, 2022 at 16:02
  • I have changed it, but it still doesn't work
    – Dev
    Commented Aug 7, 2022 at 20:42
  • @Dev Update your question's snippet with what you're currently using. Copy-paste your code. Don't type it. Commented Aug 7, 2022 at 23:01

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.