When I receive this JSON data as a response, I attempt to iterate through it inside the "component template attribute" in order to display the content within an "li" tag HTML.
{
"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/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="92f3e2e2fef7bfe6fde7f1fabffbf1fdfcd2a0bce2fcf5">[email protected]</a>",
"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/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f8998888949dd58c978d9b90d5919b9796b8cad688969f">[email protected]</a>",
"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"
}
]
}
The JS code snippet is provided below:
@Component({
selector: 'http-test',
template: `
<ul>
<li *ngFor="#data of httpData>{{data.items.related_sites[0].name}} //attempting to iterate through 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();
}
}
I've made attempts but have been unable to achieve my desired outcome. Any assistance would be greatly appreciated. Thank you.