Typeahead : Could we get an example with an actual async observable?
I had been using the typeahead from ng2-bootstrap 1.0.17, but 1.0.23 broke it. I was using a debounced switch map as follows:
this.discussionResultsObservable = this.discussionSearchTermStream
.debounceTime(300)
.distinctUntilChanged()
.switchMap((term: string) => this.discussionService.searchDiscussions(term));
This is preferable to one that fetches on every keystroke. But now, the example on this page doesn't have a real async observable version, its just creating and filtering one from static code, and I can't figure out how to get it to work with an actual one. Couldn't you guys create a static json page, possibly either pointing to a raw file on github, or somewhere else, to be able to test and see what the code would look like for an actual observable?
Thanks.
Based on the example , I did this:
constructor(service : Service) {
this.dataSource = Observable.create((observer:any) => {
this.service.method(this.typeheadModel)
.subscribe((result : any ) => {
observer.next(result);
});
});
}
Combined with [typeaheadWaitMs]="300"
This helps you?
Yes this does, I'll test this out later today. It should be in the docs too. I never knew typeaheadWaitMs existed.
@tchoulihan The docs are not up to date, I'm sorry. I will do this on the weekend.
Thanks @Dinistro .
@nereujunior Thanks, I got it working!
This is a side question, and maybe I should open up another issue for it but:
How do we get the loading and no results to show up in the typeahead dropdown? I can get them to render elsewhere, but not in the dropdown.
@tchoulihan The sample was updated a little bit (#745). I hope you see how you can use it now.
Sure, looks good to me.
@nereujunior Can you post your complete code for typeahead async working with a service? I mean the complete html and ts code?
@noorahmed94 An example is here: http://valor-software.com/ng2-bootstrap/#/typeahead
@nereujunior @Dinistro worked for me !! helped a lot. thanks!!
Hi Guys, I am getting error "TypeError: matches.slice is not a function".
Am i doing anything wrong?
app.component.ts
public constructor(private freightService: FreightService) {
this.dataSource = Observable.create((observer: any) => {
this.freightService.getMatchingProduct(this.asyncSelected).subscribe((result: any) => {
observer.next(result);
})
});
}
productSearch.service.ts
getMatchingProduct(productKeyword: string): Observable <any> {
return this.http.get("app/test")
.map(this.extractData);
}
private extractData(res: Response) {
let body = res.json();
return body.data || { };
}
typeahead-demo.html
<form [formGroup]="myForm">
<input formControlName="state"
[(ngModel)]="asyncSelected"
[typeahead]="dataSource"
(typeaheadLoading)="changeTypeaheadLoading($event)"
(typeaheadNoResults)="changeTypeaheadNoResults($event)"
(typeaheadOnSelect)="typeaheadOnSelect($event)"
[typeaheadWaitMs]="300"
[typeaheadOptionsLimit]="7"
[typeaheadOptionField]="'productDescription'"
placeholder="Locations loaded with timeout"
class="form-control">
</form>
I can see http response body in network tab
[{
"productDescription": "MAIL MKTG NIKLES HIGHLIGHT TUBE",
"productNumber": "10667"
}, {
"productDescription": "TEST GLASSES/VAC BREAKER BR 15MM BSP",
"productNumber": "100436"
}, {
"productDescription": "SIGHT GLASSES/VAC BREAKER BR 15MM BSP",
"productNumber": "100438"
}]
It looks good to me so far, except try doing this in the ngOnInit instead of the constructor.
Thanks tchoulihan for responding. Found problem, below code was mapping Observable to undefined.
private extractData(res: Response) {
let body = res.json();
return body.data || { };**_
}
"body.data" is undefined. It should be "body"
this.dataSource = Observable.create((observer: any) => {
this.freightService.getMatchingProduct(this.asyncSelected).subscribe((result: any) => {
observer.next(result);
})
});
@sourabh1983 Im almost two years too late x) but what should i do in the error case of the respond. Its abviously ugly if the console throws me an error if the result is not found, since the user could type anything into the input^^
@MarcoLeko I think you would use something like this inside the subscribe method:
if (result) {
observer.next(result);
}
observer.error(new Error('ERRORRR!'));
Upgraded to Angular 7 and it stopped working.
this.dataSource is undefined.
this.dataSource = Observable.create((observer: any) => { this.service.getAreaSearchByText(this.placeNameText) .subscribe((result: any) => { this.searchResults = <AreaTextSearchResult[]>result; let newResult = _.map(this.searchResults, function (result) { return new AutoCompleteResult(result.PolygonAreaCode, result.PlaceName, result.PolygonAreaName); }); observer.next(newResult); }); });
Resolved the issue by upgrading the ngx-bootstrap version to 3.2.0