Angular 5 with .NET (C#)
I am trying to incorporate the Typeahead control provided by ng-bootstrap. Given below is the requirement: - A typeahead control called Category - On selecting one of the items in the Category list, another control should be populated (Subcategory)
My doubt is:
How do I get the typeahead to accept a key/value pair (or like an object eg: Category with Id, Name as its properties) so that I can get the key corresponding to the category value that is clicked. This categoryid will then be used to populate the subcategory field.
Category
The above given is the control that I am using and the .ts file has the following:
@ViewChild('category') category: NgbTypeahead;
focus$ = new Subject<string>();
click$ = new Subject<string>();
search = (text$: Observable<string>) =>
text$
.debounceTime(200).distinctUntilChanged()
.merge(this.focus$)
.merge(this.click$.filter(() => !this.category.isPopupOpen()))
.map(term => (term === '' ? this.categories : this.categories.filter(v => v.toLowerCase().indexOf(term.toLowerCase()) > -1)).slice(0, 10));
The 'categories' field gets populated by extracting the Category.Name from an api call which actually returns a Category object with Name and Id.
Currently, I am getting the value and then finding the id corresponding to it in the Category object that was returned by the api. But this results in maintaining a lot of extra objects.
Started working on Angular5 (Angular for that matter) just a week back. Any inputs are greatly appreciated.
This was done by referring the example at: https://ng-bootstrap.github.io/#/components/typeahead/api