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

相关文章
相关标签/搜索