对象数组上的角度输入搜索字段

时间:2019-02-25 09:57:03

标签: angular search

嘿,我是新手,我有一个问题 我有一个对象数组:

myObject
    .myMethod()

在我的搜索字段中,输入customerNumber,如果持有人存在,则其名称将显示在该字段下;如果不存在,它将显示错误(该持有人不存在) 组件:

[navigationViewController popToRootViewControllerAnimated: YES];

和服务:

holders: Holder[] = [
  { name: 'hanna', nationalId: 310, customerNumber: '123' },
  { name: 'jack', nationalId: 320, customerNumber: '124' },
  { name: 'sara', nationalId: 320, customerNumber: '125' },
];

有没有更好的解决方案?而且我想处理在组件中而不是在服务中存在持有人

1 个答案:

答案 0 :(得分:0)

如果愿意,可以将整个内容放入组件中,但这是一种更好的方法。

如果要在组件中编写整个代码,则可以通过这种方式完成。

constructor(){
  this.searchTerm$.pipe(
    debounceTime(500),
    distinctUntilChanged(),
    map(term => this.searchEntries(term))
    .subscribe(results => {
       this.result = results;
     });
  )
}

searchEntries(term){
    if(term){
        this.searchResult = this.holders.filter(x => x.customerNumber === term);
        if(this.searchResult[0]){
            return this.searchResult[0].name;
        }else{
            return 'the holder does not exist';
        }
    }else{
        this.search = term;
    }
}