Json數據不顯示。我正在做一個httpGet,一切似乎都很好。看來數據被加載到我的數組中,並且有3個對象。然而,沒有任何內容顯示在HTML,{{associateList}}中,或者當我執行「console.log」時。我必須把它管到JSON {{associateList | JSON}}然後我得到了所有東西,當然這一切都在一個大塊。 我似乎無法訪問數組中的單個對象,我似乎無法弄清楚該怎麼做。角度2 HTTP請求json數據不顯示不使用| JSON
這是我獲得服務:
getAssociatesData(){
return this.http.get('http://Api/Associates').map(res =>res.json());
這是我如何訂閱和其他的東西:
associateList: associateData[];
this.GetDataServices.getAssociatesData().subscribe(
associates =>{this.associateList= (associates)},
error => this.serviceError=error,
() => {
this.formatAssociates();
console.log('associates loaded');
//this.isLoading.next(false)
}
);
formatAssociates(){
let associateCount = this.associateList.length;
console.log('number of associates: ' + associateCount);
this.showAssociates = true;
}
interface associateData{
id : number;
firstName : string;
lastName : string;
middleName : string;
fullName : string;
}
而在HTML:
<div *ngIf="showAssociates">
<span *ngFor="let associate of associateList; let count=index">
<div id={{count}} >
associate# = {{count}}
associateID = {{(associate | async).id}}
First Name = {{associate.firstName}}
Last Name = {{associate.lastName}}
Middle Name = {{associate.middleName}}
Full Name = {{associate.fullName}}
</div>
</span>
</div>
{{associateList | json}}
的問題是,我當我將「associateList」傳遞給json時,只能看到數據。我無法顯示個別元素。
你的JSON是怎樣的?你可以發佈嗎? :) – Alex