Is there a way for me to process JSON from the template in a manner similar to the second code I provided?
First code.
This method works well when using .json
and .map ()
@Component({
..//
template: `..//
<li *ngFor="#user of users">
JSON: {{ user.name }}
..//`
get(){
this.http.get('lib/sampleResAPI.json')
.map(res => res.json())
.subscribe(users => this.users = users);
..//
}
sampleResAPI.json is formatted as follows:
[
{
"id": 362,
"name": "test",
..//
}
]
However, even though I am able to display an alert with alert(this.users.name);
, I cannot access the property from the template {{ users.name }} because it returns undefined.
-> The second code snippet was not found
@Component({
..//
template: `..//
JSON: {{ users.name }}
..//`
get(){
this.http.get('lib/sampleResAPI.json')
//.map(res => res.json())
.subscribe(users => this.users = users.json());
//this.u = JSON.parse(this.users);
alert(this.users.name);
..//
}
SampleResAPI.json does not contain square brackets:
{
"id": 362,
"name": "test",
..//
}
Is there a correct way to handle the JSON data like users.name
in the alert from within the template?