2016-09-21 36 views
1
Object {0: Object, 1: Object, 2: Object, 3: Object, 4: Object, httpStatus: 200, httpStatusText: null, httpBodyText: "[object Object],[object Object],[object Object],[object Object],[object Object]"} 

0:Object (example of object i opened to show content) 
CountryInitials : "US" 
Id: "101" 
CountryName: "United States" 
Population: 318.9 
__proto__: 
Object 
1:Object 
2:Object 
3:Object 
4:Object 

這是我的瀏覽器中的一個例子,它顯示瞭如何接收數據,所以它包含對象的對象,並且我想像它的數組一樣對待它,而且我認爲這是但它不工作....不能在html中打印一個對象列表

這是HTML:

<div *ngFor="#obj of myList> 
     <div><b>Country ID:</b> {{obj.Id}} <b>Country Name:</b> {{obj. CountryName}}}</div> 
     </div> 

和它不工作...我不知道爲什麼,我只是想呈現的清單具有國家/地區編號和國家/地區名稱的對象..

EXCEPTION:

EXCEPTION: Error trying to diff '[object Object]' in [myList in [email protected]:31] 

有人可以幫我解決這個問題嗎?

謝謝!

回答

0

您可以使用管道json打印模板中的對象:https://angular.io/docs/ts/latest/api/common/index/JsonPipe-pipe.html

@Component({ 
    selector: 'json-pipe', 
    template: `<div> 
    <p>Without JSON pipe:</p> 
    <pre>{{object}}</pre> 
    <p>With JSON pipe:</p> 
    <pre>{{object | json}}</pre> 
    </div>` 
}) 
export class JsonPipeComponent { 
    object: Object = {foo: 'bar', baz: 'qux', nested: {xyz: 3, numbers: [1, 2, 3, 4, 5]}}; 
} 
0

可以使用keyvalue打印JSON格式... 在這key是0,1,等(如你的情況)和value是對象(如你的情況)...