For example this is my html template:
<select class="custom-select d-block w-100" id="genre" name="genre"
[(ngModel)]="film.genre"
#genreInput="ngModel"
required>
<option value="">Choose...</option>
<option *ngFor="let genre of genres;" [value]="genre.value">{{genre.name}}</option>
</select>
This is the field that binded with template from my Component:
// Genres of films like action or drama that will populate dropdown list.
genres: Genre[];
I fetch genres of films from server dynamically. In order do communicate with server I have created FilmService
This is the method which communicate server:
fetchGenres(): Observable<Genre[]> {
return this.client.get(WebUtils.RESOURCE_HOST_API + 'film' + '/genre') as Observable<Genre[]>;
}
Why this method returns Observable<Genre[]>
not something like Genre[]
?
JavaScript is async
and it does not wait for a method to return value after an expensive process. With expensive I mean a process that take a time to return value. Like fetching data from server. So you have to return reference of Observable and subscribe it.
For example in my Component :
ngOnInit() {
this.filmService.fetchGenres().subscribe(
val => this.genres = val
);
}