0

我有这个功能,我将显示数组值,如果输入框中输入的值大于 1,它将比较并显示值。我如何在自定义过滤器中重写它。

  users: searchInputModel[] = [
    {
      name: 'Jack Daniel',
      code: 'AD1213'
    },
    {
      name: 'John Doe',
      code: 'TFQUN'

    }
    ];

constructor() {
    this.userControl.valueChanges.subscribe((user) => {
      if (user && user.length > 1) {
        this.filteredUsers = user ? this.filterUsers(user) : this.users.slice();
      }
    });
  }

  filterUsers(value: string): searchInputModel[] {
    const filterValue = value.toLowerCase();
    return this.users.filter(user => user.name.toLowerCase().indexOf(filterValue) === 0);
  }
4

1 回答 1

0

你可以试试:

this.userControl.valueChanges.pipe(
   filter(user => user && user.length > 1),
   map(e => this.filterUsers(e))
).subscribe(filteredUsers => this.filteredUsers = filteredUsers);
于 2019-08-28T09:39:10.840 回答