I was trying to load dropdown list from http response using angular2 service.
Here is my service method :
categories: any;
getCategories() {
let subscription = this.httpclient
.get(this.server_url)
.subscribe((data) => {
this.categories = data;
console.log(this.categories);
});
}
Component TS
categoryID: number;
constructor(public categoryservice: CategoryService) { }
ngOnInit() {
this.categoryservice.getCategories();
}
And component html code is :
<select data-width="20%" class="selectpicker" [(ngModel)]="categoryID">
<option *ngFor="let category of categoryservice.categories" value= {{category.categoryId}} >{{category.categoryName}}</option>
</select>
But since the response in getCategories() method takes a while, dropdown list is empty. If the response comes fast list loads properly.
Can anyone please help me in this scenario when response takes time to fetch data from server? How I can load the list after reponse comes?