Jimmy Jimmy - 5 months ago 21
JSON Question

how do i get id of these JSON response in angular

<label ng-repeat="role in roles">
<input type="checkbox" data-checklist-model="user.roles" data-checklist-value="role"> {{role.text}}
</label>

$scope.roles = [
'guest',
'user',
'customer',
'admin'
];

<div class="col-xs-12 col-sm-6">
<h5>G2 Result</h5>
<pre>{{user.roles|json}}</pre>
</div>


i used checklist-model js if i printing the roles Getting below reponse but i want to get id of below these json response

[
{
"id": 2,
"text": "user"
},
{
"id": 3,
"text": "customer"
},
{
"id": 4,
"text": "admin"
},
{
"id": 1,
"text": "guest"
}
]


how do i get id of these JSON response in angular

I wish to pass id into data-checklist-model

Answer

roles is a JSONArray. You need to provide index to get the id of concerned JSONObject.

Like:

user.roles[0].id or user.roles[1].id

If you want to get id of all JSONObjects then you need to use ng-repeat and iterate this array to get concerned JSONObject.

Like:

<div ng-repeat="role in user.roles">             
    <h2>{{role.id}}</h2>
</div>
Comments