Reputation: 99
Trouble accessing the _embedded property in Json in Angular. Is it even possible? Json response created using Spring-Data-Jpa looks like this:
{
"_embedded": {
"reviews": [
{
"author": "Anonymous",
"content": "This movie sucks",
"upvotes": 0,
"downvotes": 0,
"_links": {
"self": {
"href": "http://localhost:8080/api/reviews/1"
},
"review": {
"href": "http://localhost:8080/api/reviews/1"
},
"film": {
"href": "http://localhost:8080/api/reviews/1/film"
}
}
}, ...
]
},
"_links": {
"self": {
"href": "http://localhost:8080/api/reviews{?page,size,sort}",
"templated": true
},
"profile": {
"href": "http://localhost:8080/api/profile/reviews"
}
},
"page": {
"size": 20,
"totalElements": 6,
"totalPages": 1,
"number": 0
}
} From my Client app I would like to process reviews nested in the embedded field. However I cannot get to the _embedded property. My Angular Service looks like this:
getEmbedded(): Observable<any> {
return this.http.get(ServerConfig.serverAddress + '/api/reviews');
}
Component using the service:
export class ReviewListComponent implements OnInit {
embeddedReviewResource: any;
constructor(private reviewService: ReviewService) { }
ngOnInit() {
this.reviewService.getEmbedded().subscribe(data => {
this.embeddedReviewResource = data;
});
}
I tried referncing the _embedded property in a number of ways in my view:
*ngFor="let review of embeddedReviewResource.reviews
or
*ngFor="let review of embeddedReviewResource._embedded.reviews
or
*ngFor="let review of embeddedReviewResource.getEmbedded().reviews
or
*ngFor="let review of embeddedReviewResource._embeddedViews.reviews
None of theses seems to do the trick. How can I access _embedded property in the Json File? Full source code is available at my GitHub.
Upvotes: 0
Views: 3023
Reputation: 1794
reviews = [];
this.reviewService.getEmbedded().subscribe(data => {
this.reviews = data._embedded.reviews;
});
template
<div *ngFor="let review of reviews">
{{ review.content }}
</div>
Upvotes: 2