JSON对象列表的Angular 6 ngbTypeahead过滤器

yam*_*585 5 typeahead angular-ui-bootstrap angular

我有一个ngbTypeahead,它在字段中键入时应该能够查询对象列表。列表“投影”中对象的形式为

{
    code: "6326"
    group: "world"
    name: "WGS 1984"
}
Run Code Online (Sandbox Code Playgroud)

HTML:

<input id="typeahead-basic" type="text" class="form-control" [(ngModel)]="item.outputProjection.name" [ngbTypeahead]="search" />
Run Code Online (Sandbox Code Playgroud)

在component.ts类中搜索过滤器代码:

    search = (text$: Observable < string > ) =>
    text$.pipe(
        debounceTime(200),
        distinctUntilChanged(),
        merge(this.focus$),
        map(term => (term === '' ? this.projections :
            this.projections.filter(v => v.name.toLowerCase().indexOf(term.toLowerCase()) > -1)).slice(0, 10))
    )
Run Code Online (Sandbox Code Playgroud)

当我输入时,我得到的是:

在此处输入图片说明

但是我想显示的是json对象中的名称。

另外,bootstrap的人也关闭了它并说这不是错误,而是功能请求:

https://github.com/valor-software/ngx-bootstrap/issues/749

无论平台如何,大多数现代UI控件都可以使用对象。

sha*_*jan 7

将格式化程序添加到您的代码中

formatter = (result: string) => result.name;
Run Code Online (Sandbox Code Playgroud)

另外,将格式化程序添加到模板中。

<input id="typeahead-format" type="text" class="form-control" [(ngModel)]="model" [ngbTypeahead]="search" [resultFormatter]="formatter" />
Run Code Online (Sandbox Code Playgroud)

  • @shajan我认为格式化程序中的结果类型应该是any,而不是字符串 (2认同)