我正在尝试使用ng-bootstrap的NgbTypeahead,但未显示带有选项的下拉列表。
我很确定自己做错了事,搜索之后,我的input
元素将ng-reflect-ngb-typeahead="function (text) {
显示为属性,好像它无法识别search
函数一样
我在 component.ts 中的代码:
search = (text: Observable<string>) => {
return text.pipe(
debounceTime(200),
distinctUntilChanged(),
map(term => {
console.log(this.livros.filter((v) => v.titulo.toLowerCase().indexOf(term.toLowerCase()) > -1).slice(0, 10));
term.length < 3 ? [].slice() : this.livros.filter((v) => v.titulo.toLowerCase().indexOf(term.toLowerCase()) > -1).slice(0, 10);
})
);
}
摘录自 component.html :
<div class="row">
<div class="col-md-12">
<label for="livro-search">Digite o título do livro</label>
<input id="livro-search" type="text" class="form-control" [(ngModel)]="model" [ngbTypeahead]="search">
</div>
</div>
运行livros
时会填充ngOnInit
字段,因此当我尝试搜索时该字段不会为空。
控制台会正确输出结果数组,因此我知道search
函数应该可以正常工作。但是我无法用响应填充组件。
我正在使用:
如果有人能阐明我的失败之处,我将永远感激不已(至少到年底为止)。
答案 0 :(得分:1)
您的地图函数不返回任何内容。
search = (text: Observable<string>) => {
return text.pipe(
debounceTime(200),
distinctUntilChanged(),
map(term => {
console.log(this.livros.filter((v) => v.titulo.toLowerCase().indexOf(term.toLowerCase()) > -1).slice(0, 10));
return term.length < 3 ? [].slice() : this.livros.filter((v) => v.titulo.toLowerCase().indexOf(term.toLowerCase()) > -1).slice(0, 10);
})
);
}
答案 1 :(得分:0)
让我们尝试一下:
ngb-typeahead-window {
display: block!important;
}