ionViewWillEnter不会触发(离子2,角度2,打字稿)

时间:2017-04-26 19:53:27

标签: angular typescript ionic2 ion

我将正确的数据传递给this.items,但切换到视图后,当ionViewWillEnter方法通常应该触发时,它不会出现。相反,它是在我的搜索栏中输入内容之后做的

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 个答案:

答案 0 :(得分:1)

请记住,Http请求(您在ionViewWillEnter()函数中使用的)是异步。这意味着请求范围之外的任何代码都将继续运行应用程序向服务器发出请求。

您在生命周期事件结束时将this.data分配给this.items,但您在Http请求之外执行此操作。而是在data =>回调中进行分配。

相关问题