首页 > 解决方案 > ngbtypeahead angular 4 返回属性“长度”不存在类型“{}”错误

问题描述

我想在其中一个文本字段中实现自动完成。我正在尝试按照这个简单的示例使用引导输入。

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">

组件.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))
)
  }

请帮忙。

标签: angularautocomplete

解决方案


由于 term 不是字符串,您可以在箭头函数的开头定义它,如下所示:

  map(term => term.length < 2 ? []
    : states.filter((v:string) => v.toLowerCase().indexOf(term.toLowerCase()) > -1).slice(0, 10))
) 

推荐阅读