使用http進行請求之后返回的數據有更新,但是綁定頁面的數據並沒有刷新,經過查找之后 發現可以使用變更檢測 ChangeDetectorRef 來進行檢測刷新。
官方文檔說明 : ChangeDetectorRef
應用代碼如下:
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(); 不然的話頁面的數據仍然不會刷新。
end!