I just started working on angular 2 and exploring various of its components. I am getting data response to show on html from server in JSON format. In angular 1.x we can directly hold json object in variable and use its directly on html with the use of '.' operator without defining all variables. My question is:
- Can we hold json object receive from server through ajax in any variable in component?
- If yes, Then how can we use it on HTML without defining each variable available on that json response?
I am trying to implement is similar like showing below.
{
"data": {
"usersDetails": [
{
"image": "../userimage.jpg",
"projects": [
{
"project_id": "1",
"name": "test_project",
"status": "0"
}
],
"created_on": "1480935474",
"name": "Test name",
"id": "10",
"username": "tester.user"
}
]
},
"message": "OK",
"status": 200
}
In component:
user = response.data.userDetails[0];
In HTML
<tr>
<td>User name</td>
<td>{{user.name}}</td>
</tr>
<tr>
<td>User image</td>
<td><img src="{{user.image}}"/></td>
</tr>
<tr>
<td>User username</td>
<td>{{user.username}}</td>
</tr>
<tr>
...
</tr>