ion-searchbar事件无法正常运行

时间:2019-08-31 21:02:44

标签: javascript angular typescript ionic-framework search

当我在ion-searchbar中搜索时,它会在几秒钟内列出我要查找的内容,但是紧随其后重新加载主列表。

home.page.html

<ion-searchbar mode="ios" (ionInput)="getItems($event)" animated></ion-searchbar>

home.page.ts

export class HomePage implements OnInit{
unit = [];
  constructor( private unitService: UnitService) { this.loadUnit();}

  ngOnInit(){
    this.loadUnit();
  }
  loadUnit(){
    this.unitService.getUnit().subscribe(res=>{
      this.unit= res;
    })
  }

   getItems(ev) {
    this.loadUnit();
    let val = ev.target.value;

    if (val && val.trim() != '') {
      this.unit = this.unit.filter((un,name) => {
        return (un.name.toLocaleLowerCase().indexOf(val.toLocaleLowerCase()) > -1);
      })
    }
    }
}

unit.service.ts

  getUnit(){

      return this.http.get(`${this.baseUrl}/units`).pipe(
      map((unit: any[]) => {
        return unit.map((un, index) => {
          un.unIndex = index + 1;
          return un;
        });
      })
    )
  }

0 个答案:

没有答案