2017-04-26 179 views
0

我將正確的數據傳遞給this.items,但切換到視圖後不會出現,此時通常應該觸發ionViewWillEnter方法。相反,它打字的東西進入我的搜索欄ionViewWillEnter不會觸發(離子2,角2,打字稿)

ionViewWillEnter方法後的作用:

ionViewWillEnter() { 
      ... 
      this.http.post('http://www.example.com/select.php', creds, { 
       headers: headers 
      }) 
      .map(res => res.json().User) 

      .subscribe(
      data => this.data = data.map(user => user.Name), 
      err => this.logError(err), 
     () => console.log('Completed') 
     ); 


     this.items = this.data; 
    } 

搜索欄:

getItems(ev) { 
    // Reset items back to all of the items 
    this.initializeItems(); // same code as ionViewWillEnter 

    // set val to the value of the ev target 
    var val = ev.target.value; 

    // if the value is an empty string don't filter the items 
    if (val && val.trim() != '') { 
     this.items = this.items.filter((item) => { 
     return (item.toLowerCase().indexOf(val.toLowerCase()) > -1); 
     }) 
    } 
    } 

我的html:提前

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

    <ion-list> 
    <ion-item *ngFor="let item of items"> 
     {{ item }} 
... 

感謝和問候

回答

1

請記住,Http請求(您在ionViewWillEnter()函數中使用的請求)是異步。這意味着請求範圍外的任何代碼將在應用程序向服務器發出請求時繼續運行。

您在生命週期事件結束時將this.data指定爲this.items,但是您在Http請求外執行此操作。相反,請在data =>回調中進行分配。