Tôi có một thành phần đơn giản gọi api REST mỗi vài giây và nhận lại một số dữ liệu JSON. Tôi có thể thấy từ các báo cáo nhật ký của mình và lưu lượng truy cập mạng rằng dữ liệu JSON được trả về đang thay đổi và mô hình của tôi đang được cập nhật, tuy nhiên, chế độ xem không thay đổi.
Thành phần của tôi trông như:
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());
}
}
Và quan điểm của tôi trông như:
<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>
<tbody >
<tr *ngFor="#detected of recentDetections">
<td>{{detected.broadcastId}}</td>
<td>{{detected.vendor}}</td>
<td>{{detected.timeStamp | date:'yyyy-MM-dd HH:mm:ss'}}</td>
<td>{{detected.macAddress}}</td>
</tr>
</tbody>
</table>
</div>
Tôi có thể thấy từ kết quả của console.log(this.recentDetections[0].macAddress)
đối tượng RecentDetections đang được cập nhật, nhưng bảng trong dạng xem không bao giờ thay đổi trừ khi tôi tải lại trang.
Tôi đang đấu tranh để xem những gì tôi đang làm sai ở đây. Có ai giúp được không?