1

如何读取 (Firebase & FirebaseIndex) 对象?

我的 Ctrl 看起来像:

var url = 'https://MYFIREBASE.firebaseio.com/';
var toDoRef = new Firebase(url);
var index = 'podio_covers/'+ $routeParams.label_id +'/limesurveys/';
var master = 'podio_surveys';

var surveys = new FirebaseIndex(toDoRef.child(index), toDoRef.child(master))
$scope.surveys = angularFireCollection(surveys);

console.log ($scope.surveys) 看起来像:

[add: function, remove: function, update: function]
0: angularFireItem
    $id: "855243"
    $index: 0
    $ref: J
    current_revision: Object
    fields: Object
    initial_revision: Object
    item_id: 51176724
    last_event_on: "2013-06-09 18:56:25"
    limesurvey_data: Object
    ratings: Object
    rights: Array[10]
    title: "855243"
    __proto__: angularFireItem
add: function (item, cb) {
length: 1
remove: function (itemOrId) {
update: function (itemOrId) {
__proto__: Array[0]

现在 - 我必须遍历所有结果。$scope.surveys.length => 不起作用

阅读 $scope.surveys.0.title =>错误!我看到了 - [add: function, remove: function, update: function] - 有没有办法阅读 $scope.surveys.0.title ?


编辑1: @Kato(希望更清楚)

在这里你可以看到我当前的 Ctrl 尝试:

var url = 'https://XXXXX.firebaseio.com/';
var toDoRef = new Firebase(url);
    var index = 'podio_covers/'+ $routeParams.label_id +'/limesurveys/';
    var master = 'podio_surveys';

    var surveys = new FirebaseIndex(toDoRef.child(index), toDoRef.child(master))
    $scope.surveys = angularFireCollection(surveys);
    console.log ('surveys[0]');         
    console.log ($scope.surveys[0]); **=> Undefined**

    console.log ('childRefs');          
    console.log (surveys.childRefs);


    var count = $scope.surveys.length; 
    console.log (count); **=> Undefined**


//--------------------------------------------------------------
// LOOP trough all surveys.
//--------------------------------------------------------------


for (var i = 0; i < count; i++) {

        console.log ($scope.surveys[i]);

            // do something with $scope.surveys[i]

}

编辑2:

    $timeout(function() {
            // Waiting until Data is loaded.

        count = $scope.surveys.length;
            console.log (count);

            //Do Loop here

    }, 1500;
4

2 回答 2

2

这将起作用:

$scope.surveys = $firebase(new Firebase(yourFBUrl));

$scope.surveys.$on("loaded", function() {
   var keys = $scope.surveys.$getIndex();
   console.log("count: " + keys.length); 
});
于 2014-02-16T00:50:39.333 回答
0

有点不清楚错误是什么;包含试图阅读调查的代码可能会有所帮助。您是在 JavaScript 中还是使用 ng-repeat 指令来执行此操作?

从您的问题中我可以看出,您只需要更改$scope.surveys.0$scope.surveys[0]即可进入第一个元素。当然,您需要等到从 Firebase 获取数据后才能访问它。

但实际上,您可能想要的是在您的 HTML 中更像这样的内容:

<ul ng-controller="Ctrl">
   <li ng-repeat="survey in surveys">{{survey.title}}</li>
</ul>

更新

根据您对问题的补充,我可以看到您在数据完全加载之前正在访问数据。该数据是异步加载的,因此 angularFireCollection() 之后的 JavaScript 将在结果到达之前运行。

当你使用像 ng-repeat 这样的 Angular 函数时,它会观察值的变化并等待数据加载后再渲染;你想在这里做同样的事情。

var surveys = new FirebaseIndex(toDoRef.child(index), toDoRef.child(master))
$scope.surveys = angularFireCollection(surveys);

// this will be zero! data hasn't come back yet
console.log($scope.surveys.length);

// wait for changes
$scope.$watch('surveys', function() {
   console.log( $scope.surveys.length ); // greater than zero
   console.log( 'first entry', $scope.surveys[0] ); // no longer undefined
});
于 2013-06-26T05:06:16.440 回答