将组件绑定ng-select
到 的结果时Array.filter()
,从下拉列表中选择一个选项会导致页面变得无响应。
是否有正确的方法绑定到过滤的项目数组?
import { Component } from '@angular/core';
interface IListItem {
id: number;
name: string;
}
@Component({
selector: 'my-app',
template: `
<ng-select
[items]="items"
bindLabel="name"
placeholder="Select item..."
[(ngModel)]="selectedItem">
</ng-select>`
})
export class AppComponent {
private readonly _items: IListItem[] = [
{ id: 1, name: "One" },
{ id: 2, name: "Two" },
{ id: 3, name: "Three" },
{ id: 4, name: "Four" },
{ id: 5, name: "Five" },
{ id: 6, name: "Six" },
{ id: 7, name: "Seven" },
{ id: 8, name: "Eight" },
{ id: 9, name: "Nine" },
{ id: 10, name: "Ten" }
];
get items(): IListItem[] {
return this._items.filter(i => i.id % 2 === 0);
}
selectedItem: IListItem;
}