以下是我正在嘗試基於Angular Tutorial進行工作的內容。直到任何對象被點擊後,角度2纔會更新
export class DashboardComponent implements OnInit {
title = 'Current Robots';
private sobots: Robot[];
constructor(
private router: Router,
private sobotService: RobotService) {
}
getRobots() {
this.sobotService.getRobots().then(sobots => Array.prototype.push.apply(this.sobots, sobots.data));
}
ngOnInit() {
this.getRobots();
}
gotoDetail(sobot: Robot) {
let link = ['/detail', sobot.Id];
this.router.navigate(link);
}
和視圖
<h3>{{title}}</h3>
<div class="grid grid-pad">
<div *ngFor="let sobot of sobots" (click)="gotoDetail(sobot)" class="col-1-4">
<div class="module sobot">
<p>HostKey</p>
<h4>{{sobot.HostKey}}</h4>
</div>
</div>
</div>
<sobot-search></sobot-search>
sobots.data看起來是返回預期的數據對象 - 但它仍然沒有更新,直到我點擊任何按鈕/路由/任何事件。
沒有錯誤在控制檯中顯示出來,我很困惑!
我已經先行一步,並試圖確保它只是將數據添加到原數組對象,但即使這樣似乎並沒有工作!
我會擴展這個答案,包括如何利用NgZone。以下文章完美地解釋了它。 https://blog.thoughtram.io/angular/2016/02/01/zones-in-angular-2.html –