I am using ng-select with Angular 7 and have the following component:
export class PostComponent {
selectedCategoryId: number;
categories$: Observable<CategoryModel[]>;
constructor(private categoryService: CategoryService) { }
ngOnInit() {
this.categories$ = this.getCategories('');
}
getCategories(term: string): Observable<CategoryModel[]> {
return this.categoryService.get(term).pipe(map((envelope: Envelope<CategoryMessage.Response>) =>
envelope.result.map((response: CategoryMessage.Response) => {
return {
id: response.id,
name: response.name
};
})));
}
}
The categoryService
returns Categories
where Category Name
contains the term.
On the template I have the following:
<ng-select
class="select"
[items]="categories$ | async"
[addTag]="true"
bindLabel="name"
bindValue="id"
[multiple]="false"
[(ngModel)]="selectedCategoryId">
</ng-select>
Question
How can I call the API, through the service, only when the typed word has 3 or more letters? This would be to prevent getting thousands of categories.
How can I integrate my code to use typeahead?