我一直在這個問題上今天一直在努力,似乎無法弄清楚我做錯了什麼。我確定它很簡單,我可以忽略,但任何幫助將不勝感激。初始呈現* ngFor與Http響應數據問題
所以這裏的問題。我有一個簡單的組件,應該使用* ngFor來呈現應用程序id的列表。這個列表是從web api中檢索的。 http請求按預期工作,我在網絡上看到了正確的響應。
當結果返回時,我將數組賦值給組件中的局部變量,並且我會假設* ngFor會識別出變化並適當地繪製屏幕。
我看不到瀏覽器控制檯中列出的客戶端錯誤。這也是Angular 2 RC5。
應用list.component.html
<h3>Applications</h3>
<ul>
<li *ngFor="let app of applications">{{ app.id }}</li>
</ul>
APP-list.component.ts
import { Component, OnInit } from '@angular/core';
import { Application } from './application';
import { ApplicationService } from './application.service';
@Component({
selector: 'sng-application-list',
template: require('./application-list.component.html'),
providers: [ ApplicationService ]
})
export class ApplicationListComponent {
errorMessage: string;
applications: Application[] = [];
constructor(private applicationService: ApplicationService) { }
ngOnInit() {
this.getApplications();
}
getApplications() {
this.applicationService.getApplications()
.subscribe(
applications => {
this.applications = applications
},
error => {
this.errorMessage = <any>error;
});
}
}
在瀏覽器中沒有錯誤?我看到你沒有導入應用程序,但你已經定義了:applications:Application [] = []; –
這是複製粘貼錯誤。 – Cobster
'{{app?.id}}'試試這個。內* ngFor嘗試'{{applications | json}}'並告訴我你有沒有獲得任何數據? – micronyks