循环访问Firebase和Firebase Index的结果

问题描述 投票:1回答:2

如何阅读(Firebase和Firebase索引)对象?

我的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;
firebase angularfire
2个回答
2
投票

这将有效:

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

$scope.surveys.$on("loaded", function() {
   var keys = $scope.surveys.$getIndex();
   console.log("count: " + keys.length); 
});

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>

UPDATE

根据您对问题的补充,我可以看到您在完全加载之前访问数据。这些数据是异步加载的,因此在结果到达之前,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
});
© www.soinside.com 2019 - 2024. All rights reserved.