Angular ngBootstraps typeaheade导致错误:找不到不同的支持对象';[目标承诺]';类型为';对象';
我在使用ngBootstraps typeahead处理异步http请求时遇到问题 我得到的是 example.htmlAngular ngBootstraps typeaheade导致错误:找不到不同的支持对象';[目标承诺]';类型为';对象';,angular,typescript,asynchronous,typeahead,ng-bootstrap,Angular,Typescript,Asynchronous,Typeahead,Ng Bootstrap,我在使用ngBootstraps typeahead处理异步http请求时遇到问题 我得到的是 example.html <input id="typeahead-basic" type="text" class="form-control" value="{{issue.Series.Title}}" (selectItem)="selectSeries($event)" [ngbTypeahead]="searchSeries" [resultFormatter]="seriesFor
<input id="typeahead-basic" type="text" class="form-control" value="{{issue.Series.Title}}" (selectItem)="selectSeries($event)" [ngbTypeahead]="searchSeries" [resultFormatter]="seriesFormatter"/>
据我所知,错误消息ngFor需要一个阵列才能工作。我对该错误消息的问题是,searchSeries确实返回了一个数组。如果我将searchSeries更改为
searchSeries = (text$: Observable<string>) =>
text$
.debounceTime(100)
.distinctUntilChanged()
.map(async term => {
let result = await this.service.getSeries(term);
console.log(result);
return result;
});
searchSeries=(文本$:可观察)=>
正文$
.debounceTime(100)
.distinctUntilChanged()
.map(异步术语=>{
让result=wait this.service.getSeries(term);
控制台日志(结果);
返回结果;
});
我可以看到结果包含一个数组。但是为什么我会收到这个错误消息呢
提前谢谢 首先,我希望您使用RXJS和observable。 关于错误消息:问题在以下行中: 让result=wait this.service.getSeries(term); 结果现在是一个可观察的,所以你不能在上面循环 如果您试图在不更改实现的情况下修复此问题,只需等待http请求完成,然后在其中设置结果 如果你使用的是可观测数据,你需要 订阅(数据=>{let result=data}); 但事实上,我并没有试图做出承诺。 另外,我建议您查看ngbootstrap演示页面“Wikipedia搜索”上的示例
我希望您能理解我的意思,并为我糟糕的英语感到抱歉。switchMap操作员应该能够在这里帮助您。从Ng引导示例中:
search = (text$: Observable<string>) =>
text$
.debounceTime(300)
.distinctUntilChanged()
.do(() => this.searching = true)
.switchMap(term =>
this._service.search(term)
.do(() => this.searchFailed = false)
.catch(() => {
this.searchFailed = true;
return Observable.of([]);
}))
.do(() => this.searching = false)
.merge(this.hideSearchingWhenUnsubscribed);
search=(text$:Observable)=>
正文$
.debounceTime(300)
.distinctUntilChanged()
.do(()=>this.search=true)
.switchMap(术语=>
此._服务.搜索(术语)
.do(()=>this.searchFailed=false)
.catch(()=>{
this.searchFailed=true;
([])的可观测收益率;
}))
.do(()=>this.search=false)
.merge(未订阅时此.hidesearching);
ngbootstrap
typeahead需要一个可观察的
数据,您将其转换为承诺
,这就是您面临此错误的原因
NgbTypeaheadWindow.html:5 ERROR Error: Cannot find a differ supporting object '[object Promise]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.
at NgForOf.webpackJsonp.../../../common/@angular/common.es5.js.NgForOf.ngOnChanges (common.es5.js:1659)
at checkAndUpdateDirectiveInline (core.es5.js:10891)
at checkAndUpdateNodeInline (core.es5.js:12382)
at checkAndUpdateNode (core.es5.js:12321)
at debugCheckAndUpdateNode (core.es5.js:13180)
at debugCheckDirectivesFn (core.es5.js:13121)
at Object.eval [as updateDirectives] (NgbTypeaheadWindow.html:5)
at Object.debugUpdateDirectives [as updateDirectives] (core.es5.js:13106)
at checkAndUpdateView (core.es5.js:12288)
at callViewAction (core.es5.js:12651)
searchSeries = (text$: Observable<string>) =>
text$
.debounceTime(100)
.distinctUntilChanged()
.map(async term => {
let result = await this.service.getSeries(term);
console.log(result);
return result;
});
search = (text$: Observable<string>) =>
text$
.debounceTime(300)
.distinctUntilChanged()
.do(() => this.searching = true)
.switchMap(term =>
this._service.search(term)
.do(() => this.searchFailed = false)
.catch(() => {
this.searchFailed = true;
return Observable.of([]);
}))
.do(() => this.searching = false)
.merge(this.hideSearchingWhenUnsubscribed);