如何打印从Firebase返回的对象的密钥?具体来说,我如何打印出球队的名字?如。"了不起的团队"
控制器:
app.controller('TeamsController', ['$scope', '$firebaseObject','ENV',
function($scope, $firebaseObject, ENV){
var app_id = ENV.FIREBASE_APP_ID;
var ref = new Firebase("https://"+app_id+".firebaseio.com/teams");
$scope.teams = $firebaseObject(ref);
}
]);
返回的数据是:
{"$id":"teams","$priority":null,"Awesome Team":{"created":"11-11-1111","size":3},"Team 1":{"created":"26-11-1111","size":4}}
然后在视图中:
<tr ng-repeat="team in teams">
<td>{{$id}}</td>
<td>{{team.created}}</td>
<td>{{team.size}}</td>
<td><button ng-click="">X</button></td>
</tr>
使用ng-repeat在Firebase引用上迭代应该使用$firebaseArray
而不是$firebaseObject
。当使用$firebaseArray
时,每个子对象都有一个包含对象键的$id
属性。
https://www.firebase.com/docs/web/libraries/angular/guide/synchronized-arrays.html section-meta-fields
<tr ng-repeat="team in teams">
<td>{{team.$id}}</td>
<td>{{team.created}}</td>
<td>{{team.size}}</td>
<td><button ng-click="">X</button></td>
</tr>