我不确定这是否与AngularJS有关,如果它只是JSON相关。
无论如何,让我们说,我们有以下JSON:
$scope.dataSets = { "names": ["Horace","Slughorn","Severus","Snape"],"genders": ["Male","Female"] }
现在,我使用ng-repeat指令打印上面的内容如下:
<div ng-repeat="data in dataSets> //Continue readig to know what I am expcting here </div>
我期望在< div>< / div>标签是打印“名称”和“性别”。也就是说,我希望打印JSON的键。我不知道什么是钥匙,因为在他们可以是任何东西。我如何做到这一点?
如文件所述:
(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>