ngbtypeahead angle 4返回属性'length'在类型'{}'上不存在错误

问题描述 投票:1回答:1

我想在文本字段之一中实现自动完成。我尝试通过以下简单示例使用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 '{}'.

html

  <input type="text" class="form-control"  [ngbTypeahead]="states">

component.ts

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))
)
  }

请帮助。

angular autocomplete
1个回答
0
投票

由于term不是字符串,您可以像这样在箭头函数的开头定义它:

  map(term => term.length < 2 ? []
    : states.filter((v:string) => v.toLowerCase().indexOf(term.toLowerCase()) > -1).slice(0, 10))
) 
© www.soinside.com 2019 - 2024. All rights reserved.