【问题标题】:ngbtypeahead angular 4 returns Property 'length' does not exist on type '{}' errorngbtypeahead angular 4 返回属性“长度”在类型“{}”上不存在错误
【发布时间】:2019-10-02 10:06:18
【问题描述】:

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

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

请帮忙。

【问题讨论】:

  • 我试过上面的代码。我没有遇到任何错误?
  • @wentjun 我可以知道你的角度版本吗?
  • 我将您的代码复制并粘贴到此处的堆栈闪电战链接之一中:ng-bootstrap.github.io/#/components/typeahead/examples#basic
  • 以上链接使用 Angular 7 而我使用 4 。想知道这是否有影响。
  • 嗯...它可能!尤其是 RxJS 运算符。我认为在语法方面可能存在一些差异。

标签: angular autocomplete


【解决方案1】:

因为 term 不是字符串,你可以像这样在箭头函数的开头定义它:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-03
    • 2020-11-06
    • 2018-04-15
    • 1970-01-01
    • 2020-03-27
    • 1970-01-01
    • 2018-04-13
    • 2018-10-04
    相关资源
    最近更新 更多