我有一個(gè)簡單的組件,該組件每隔幾秒鐘調(diào)用一次REST API,并接收一些JSON數(shù)據(jù)。從我的日志語句和網(wǎng)絡(luò)流量中,我可以看到返回的JSON數(shù)據(jù)正在更改,并且我的模型正在更新,但是視圖沒有更改。我的組件看起來像:import {Component, OnInit} from 'angular2/core';import {RecentDetectionService} from '../services/recentdetection.service';import {RecentDetection} from '../model/recentdetection';import {Observable} from 'rxjs/Rx';@Component({ selector: 'recent-detections', templateUrl: '/app/components/recentdetection.template.html', providers: [RecentDetectionService]})export class RecentDetectionComponent implements OnInit { recentDetections: Array<RecentDetection>; constructor(private recentDetectionService: RecentDetectionService) { this.recentDetections = new Array<RecentDetection>(); } getRecentDetections(): void { this.recentDetectionService.getJsonFromApi() .subscribe(recent => { this.recentDetections = recent; console.log(this.recentDetections[0].macAddress) }); } ngOnInit() { this.getRecentDetections(); let timer = Observable.timer(2000, 5000); timer.subscribe(() => this.getRecentDetections()); }}我的看法如下:<div class="panel panel-default"> <!-- Default panel contents --> <div class="panel-heading"><h3>Recently detected</h3></div> <div class="panel-body"> <p>Recently detected devices</p> </div> <!-- Table --> <table class="table" style="table-layout: fixed; word-wrap: break-word;"> <thead> <tr> <th>Id</th> <th>Vendor</th> <th>Time</th> <th>Mac</th> </tr> </thead>從結(jié)果中可以看到console.log(this.recentDetections[0].macAddress),recentlyDetections對象正在更新,但是視圖中的表永遠(yuǎn)不會改變,除非我重新加載頁面。我正在努力查看我在這里做錯(cuò)了什么。有人可以幫忙嗎?
3 回答

慕運(yùn)維8079593
TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超5個(gè)贊
就我而言,我有一個(gè)非常相似的問題。我正在由父組件調(diào)用的函數(shù)中更新視圖,而在父組件中,我忘記了使用@ViewChild(NameOfMyChieldComponent)。我僅僅因?yàn)檫@個(gè)愚蠢的錯(cuò)誤而損失了至少3個(gè)小時(shí)。即:我不需要使用任何這些方法:
ChangeDetectorRef.detectChanges()
ChangeDetectorRef.markForCheck()
ApplicationRef.tick()
- 3 回答
- 0 關(guān)注
- 951 瀏覽
添加回答
舉報(bào)
0/150
提交
取消