首页 文章

角度与引导类型

提问于
浏览
0

我一直试图让我的Typeahead工作:

<div *ngFor="let item of items; index as i">
    <input type="text" name="name{{i}}" [(ngModel)]="item.name" [ngbTypeahead]="searchItem" />
</div>

组件:

export class MyComponent implements OnInit {

    searchItem: any;

    ngOninit() {
        this.searchItem = (text$: Observable<string>) => text$.pipe(
            debounceTime(250),
            distinctUntilChanged(),
            switchMap((itemName: string) => itemName.length >= 2 ? this.service.getItems() ? of([])),
            switchMap((items: Item[]) => items.length > 0 ? items.reduce((acc, cur) => [...acc, cur.name], []) : [])
        );
    }
}

在Typeahead中输入2个字母后的运行时,Angular给出了以下错误:

Cannot find a differ supporting object 'xxxx' of type 'string'. NgFor only supports binding to Iterables such as Arrays.

其中'xxxx'是项目的 name 属性 .

但是如果我将 reduce 函数更改为以下内容:

switchMap((items: Item[]) => items.length > 0 ? items.reduce((acc, cur) => [[...acc, cur.name]], []) : [])

它返回一个只有一个元素是另一个数组的数组,Angular不会给我任何错误,但会在Typeahead弹出窗口的同一行显示前2个值,第二行显示第3个值 . (该服务返回3项 . )

有任何想法吗?

1 回答

  • 0

    看起来当前的Bootstrap Typeahead不适用于 switchMap 但__2215__与doc中的示例一样 .

相关问题