angular - 带角度 API 调用的 Typeahead
问题描述
我需要使用 Typeahead 在 app 中添加全局搜索,当用户键入时,它应该调用 api 并在下拉列表中显示结果,我是 angular 和 typescript 的新手,如果有代码的工作示例,请告诉我。我正在使用 Angular 10 版本
谢谢你的帮助
解决方案
您可以使用许多库。
search = (text$: Observable<string>) =>
text$.pipe(
debounceTime(300),
distinctUntilChanged(),
tap(() => this.searching = true),
switchMap(term =>
this.tagService.searchByKeyword(this.category, term).pipe(
tap(() => this.searchFailed = false),
catchError(() => {
this.searchFailed = true;
return of([]);
}))
),
tap(() => this.searching = false)
)
<input class="form-control" [placeholder]="placeholder" [(ngModel)]="value" (ngModelChange)="change($event)" [ngbTypeahead]="search" [readonly]="readonly">
像这样的东西。
推荐阅读
- c - 使用 fork 在 C 中创建多个子进程
- web-crawler - 我可以在 Stormcrawler 中按锚点或标题过滤外链吗?
- python - Numpy:对数组每行的元素应用公式
- flutter - 如何在 Flutter 中更改 BLoC 中的事件?
- flutter - 颤振 HTTP 问题
- visual-studio-code - 内联样式标记未在 html 文件中获得语法高亮显示
- splunk - 在 splunk 仪表板中,应用用户输入更改的最有效方法是什么?
- php - 解析符号字符串对以构建 sql 的 WHERE 子句的条件表达式
- jenkins - 我正在尝试在 jenkins 上运行 selenium 脚本,但出现以下错误(我还附上了我要运行的脚本)
- python - 如何将抓取的数据添加到我的列表中,我无法打印“ranking_list”并且它不能为空