我想在文本字段之一中实现自动完成。我尝试通过以下简单示例使用bootstrap typeahead。
https://ng-bootstrap.github.io/#/components/typeahead/examples#basic
但是在编译时出现以下错误。
ERROR in C:/Users/eclipse-workspace/c-UI/src/app/send-email/send-email.component.ts (33,24): Property 'length' does not exist on type '{}'.
ERROR in C:/Users/eclipse-workspace/c-UI/src/app/send-email/send-email.component.ts (34,59): Property 'toLowerCase' does not exist on type '{}'.
<input type="text" class="form-control" [ngbTypeahead]="states">
import { Component} from '@angular/core';
import {debounceTime, distinctUntilChanged, map} from 'rxjs/operators';
const states = ['Alabama', 'Alaska', 'American Samoa'];
export class SendEmailComponent {
public model: any;
search = (text$: Observable<string>) =>
text$.pipe(
debounceTime(200),
distinctUntilChanged(),
map(term => term.length < 2 ? []
: states.filter(v => v.toLowerCase().indexOf(term.toLowerCase()) > -1).slice(0, 10))
)
}
请帮助。
由于term不是字符串,您可以像这样在箭头函数的开头定义它:
map(term => term.length < 2 ? []
: states.filter((v:string) => v.toLowerCase().indexOf(term.toLowerCase()) > -1).slice(0, 10))
)