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?
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>
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)); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With