我想使用ngBootstrap for Angular 4 Typeahead来自动完成。他们用于远程数据检索的例子是使用Jsonp而不是http。在这个例子中,我一直试图找到一些更多的信息来取代Jsonp。我对Observables还不是很熟悉,所以我正在努力学习它们并更好地理解它们。使用http with ngBootstrap Typeahead for Angular 4
我已经看到这个example,但它看起来真的简单,也许(?)留下了很多......为了简单起见?
有人可以指向正确的方向,我试图找到一些使用http与ngBootstrap Typeahead的好例子。
编辑
{
"took": 15,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 9,
"max_score": 4.2456956,
"hits": [
{
"_index": "query-index",
"_type": "autocomplete",
"_id": "AVxqBE-3t2o4jx2g0ntb",
"_score": 4.2456956,
"_source": {
"suggestions": "bruce"
}
},
{
"_index": "query-index",
"_type": "autocomplete",
"_id": "AVxqBE-3t2o4jx2g0ntc",
"_score": 3.064434,
"_source": {
"suggestions": "bruce wayne"
}
},
{
"_index": "query-index",
"_type": "autocomplete",
"_id": "AVxqBE-3t2o4jx2g0ntd",
"_score": 3.064434,
"_source": {
"suggestions": "bruce willis"
}
},
编辑2
export class AutocompleteComponent {
model: any;
searching = false;
searchFailed = false;
constructor(private autocompleteService: Elasticsearch) {}
//formatMatches = (query: any) => query.hits.hits._source.suggestions || '';
//formatMatches = (query: any) => query.hits.hits || '';
formatMatches = (query: any) => <any>response.json().hits.hits || '';
search = (text$: Observable<string>) =>
//search = (text$: Observable<Suggestion[]>) =>
text$
.debounceTime(300)
.distinctUntilChanged()
//.switchMap(term =>
//Observable.fromPromise(this.autocompleteService.search(term)
.switchMap(term =>
this.autocompleteService.search(term)
.do(() => this.searchFailed = false)
.catch(() => {
this.searchFailed = true;
return Observable.of([]);
}))
.do(() => this.searching = false);
}
我想我有我的工作,但我是个不知道我究竟是怎么做到的。我用你在这里发布的例子! –