Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS: How to get the key of a JSON Object

Tags:

json

angularjs

I am unsure if this has got anything to do with AngularJS at all and if it is only JSON related.

Anyhow, let us say that we have the following JSON:

$scope.dataSets = {     "names": ["Horace", "Slughorn", "Severus", "Snape"],     "genders": ["Male", "Female"] } 

Now, I am using the ng-repeat directive to print the above as follows:

<div ng-repeat="data in dataSets>     //Continue readig to know what I am expcting here </div> 

What I expect within the <div></div> tags is to print "name" and "genders". That is, I wish to print the keys of the JSON. I have no idea what the keys are, as in they could be anything. How can I do this?

like image 644
callmekatootie Avatar asked Mar 20 '13 14:03

callmekatootie


2 Answers

As docs state it:

(key, value) in expression – where key and value can be any user defined identifiers, and expression is the scope expression giving the collection to enumerate.

<div ng-repeat="(key, data) in dataSets">   {{key}} </div> 
like image 92
Stewie Avatar answered Sep 21 '22 03:09

Stewie


for accessing Json key-value pair from inside controller in AngularJs.

for(var keyName in $scope.dataSets){              var key=keyName ;      var value= $scope.dataSets[keyName ];    alert(key)   alert(JSON.stringify(value));  } 
like image 21
Saifudheen Aroniyil Avatar answered Sep 24 '22 03:09

Saifudheen Aroniyil