Angularfire访问对象键

时间:2015-09-15 13:55:25

标签: javascript angularjs firebase angularfire

如何打印从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>

1 个答案:

答案 0 :(得分:1)

使用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>