angular2 http請求返回數據以後頁面數據沒有刷新

 

使用http進行請求以後返回的數據有更新,可是綁定頁面的數據並無刷新,通過查找以後 發現能夠使用變動檢測 ChangeDetectorRef 來進行檢測刷新。html

官方文檔說明 : ChangeDetectorRefapi

 

應用代碼以下:this

 

import {Component,NgModule,ChangeDetectorRef, OnInit}from'@angular/core';

 constructor(private cdr: ChangeDetectorRef) {}

getCommentItemsFunc() {
    this.commentService.getCommentItems(id)
      .subscribe(commentItems => {
        this.commentItems = commentItems;
        this.cdr.markForCheck();
        this.cdr.detectChanges();
      })
  }    

 

:要在 this.cdr.markForCheck(); 以後加上  this.cdr.detectChanges(); 否則的話頁面的數據仍然不會刷新。spa

 

end!code

相關文章
相關標籤/搜索