我米獲得此JSON數據作爲響應和IM試圖重複「組件模板屬性」這裏面的數據顯示,「禮」標記的HTML內容
如何遍歷JSON響應數據角2打字稿
{
"items": [
{
"aliases": [
"http://www.xyz.co",
"http://facebook.xyz.co"
],
"styling": {
"tag_background_color": "#E0EAF1",
"tag_foreground_color": "#3E6D8E",
"link_color": "#0077CC"
},
"related_sites": [
{
"relation": "meta",
"api_site_parameter": "meta.xyz",
"site_url": "http://meta.xyz.co",
"name": "Meta Stack Overflow"
},
{
"relation": "chat",
"site_url": "http://chat.xyz.co",
"name": "Stack Overflow Chat"
}
],
"markdown_extensions": [
"Prettify"
],
"launch_date": 1221436800,
"closed_beta_date": 1217462400,
"site_state": "normal",
"high_resolution_icon_url": "https://cdn.sstatic.net/Sites/sxyz/img/[email protected]",
"favicon_url": "https://cdn.sstatic.net/Sites/stackoverflow/img/favicon.ico",
"icon_url": "https://cdn.sstatic.net/Sites/xyz/img/apple-touch-icon.png",
"audience": "professional and enthusiast programmers",
"site_url": "http://xyz.co",
"api_site_parameter": "xyz",
"logo_url": "https://cdn.sstatic.net/Sites/stackoverflow/img/logo.png",
"name": "Stack Overflow",
"site_type": "main_site"
},
{
"aliases": [
"http://www.xyz.co",
"http://facebook.xyzw.co"
],
"styling": {
"tag_background_color": "#E0EAF1",
"tag_foreground_color": "#3E6D8E",
"link_color": "#0077CC"
},
"related_sites": [
{
"relation": "meta",
"api_site_parameter": "meta.xyz",
"site_url": "http://meta.xyz.co",
"name": "Meta Stack Overflow"
},
{
"relation": "chat",
"site_url": "http://chat.xyz",
"name": "Stack Overflow Chat"
}
],
"markdown_extensions": [
"Prettify"
],
"launch_date": 1221436800,
"closed_beta_date": 1217462400,
"site_state": "normal",
"high_resolution_icon_url": "https://cdn.sstatic.net/Sites/xyz/img/[email protected]",
"favicon_url": "https://cdn.sstatic.net/Sites/sxyz/img/favicon.ico",
"icon_url": "https://cdn.sstatic.net/Sites/xyz/img/apple-touch-icon.png",
"audience": "professional and enthusiast programmers",
"site_url": "http://stackoverflow.co",
"api_site_parameter": "stackoverflow",
"logo_url": "https://cdn.sstatic.net/Sites/xyzw/img/logo.png",
"name": "Stack Overflow",
"site_type": "main_site"
}
]
}
我的js代碼如下所示
@Component({
selector: 'http-test',
template: `
<ul>
<li *ngFor="#data of httpData>{{data.items.related_sites[0].name}} //trying to iterate the response data.
</li>
</ul>
`,
providers:[HTTPTestService]
})
export class HTTPTestComponent {
public httpData;
constructor (private _httpService: HTTPTestService){}
getStack(){
this._httpService.getItemData()
.subscribe(
data =>this.httpData = JSON.stringify(data),
error => alert(error),
() =>console.log("finished")
);
}
ngOnInit() {
this.getStack();
}
}
我已經試過,但我可以實現我想要什麼,請幫助我,謝謝
概述試過,越來越例外
「EXCEPTION:錯誤:未捕獲的(在承諾):EXCEPTION :TypeError:無法讀取[HTTPTestComponent @ 3:13中的responseData.items]中未定義的屬性'項目'「 –
data => {this.httpData = JSON.stringify(data); console.log(this.httpData);}, – Habeeb
我也試過,但沒有得到答案
正在關注錯誤:
EXCEPTION:錯誤:未捕獲(承諾):模板解析錯誤: 解析器錯誤:意外的令牌),預期標識符,關鍵字或在[httpData的ngFor #DATA)]柱24在串HTTPTestComponent @ 3:13(」