I'm pretty new on Angular2, and I'm trying to obtain the key/value pair from a JSON file returned by a Http request. I want to store them on the objectResource property so I can retrieve them on the template.
This is my component.ts:
export class ResourceDetailComponent implements OnInit {
objectResource: any = Array();
id:any;
sub:any;
constructor(private contentService: ContentService, private route: ActivatedRoute) {
this.sub = this.route.params.subscribe(params => {
this.id = +params['id'];
});
this.getResource(this.id);
}
ngOnInit(){}
private getResource(id:number) {
return this.contentService.getSpecificResource(id).then(res => this.objectResource.push(res));
}
}
and this is mi service.ts:
public getSpecificResource(id:number) {
return this.http.get(`http://resources.dev/v1/resources/show/${id}`)
.toPromise()
.then(res => res.json());
}
when I do console.log
on res I obtain this log, which mean the service is doing the request as expected:
Object {
id: 1,
name: "quisquam",
image: "http://lorempixel.com/480/480/?88330",
description: "Consequuntur quo provident alias aut rerum.",
author: "Marc Mann Sr."
}
I've tried to retrieve these values on the template using {{objectResource.name}}
but isn't working, and I don't know how can I access this values from the template.