【问题标题】:How to show updated data with update of model in angular 2?如何通过角度 2 中的模型更新来显示更新的数据?
【发布时间】:2017-06-07 18:14:15
【问题描述】:

我目前正在使用 Angular 2 进行分页。我正在使用 ng2-pagination 来实现我的目的。到目前为止我所做的是,我已经成功显示了通过我的 api 接收到的数据。但是当我单击下一页按钮时,通过再次调用该页面的 api 来获取下一页的数据。但是这里出现问题,在我看来,收到的数据没有得到更新。任何人都可以告诉我如何用新数据更新现有视图。我的 pagechanged 功能是,

pageChanged(event)
{
// alert(event)
this.p=event;
    this.httpService.getAllLaptops(event).subscribe(
    data => {
      const myArray = [];


      for (let key in data) {
        myArray.push(data[key]);

      }

      this.GetAllLaptop = myArray;

    }

  );

}

还有我的视图模板

  <ul>
      <li *ngFor="let item of  GetAllLaptop[2] | paginate: { itemsPerPage: 10, currentPage:p,totalItems:GetAllLaptop[1] }"> {{item.SNR_Title}} </li>
    </ul>
     <pagination-controls (pageChange)="pageChanged($event)"

     directionLinks="true"
                      autoHide="true"
                      previousLabel="Previous"
                      nextLabel="Next"
                      screenReaderPaginationLabel="Pagination"></pagination-controls>

在最初通话时,我收到了数据并将其显示在视图中。但在页面更改事件中,收到新页面的数据但视图未更新。如何更新此视图? 非常感谢任何帮助或建议。

【问题讨论】:

    标签: angular pagination


    【解决方案1】:

    解决了。那是我自己的错误。我刚用过

    this.ref.detectChanges();  
      this.ref.reattach();
    

    我的 api 存在另一个问题,每次调用都返回相同的数据。

    【讨论】:

      猜你喜欢
      • 2020-11-14
      • 1970-01-01
      • 2017-05-26
      • 2011-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多