angularfire - why can't I loop over array returned by $asArray?

前端 未结 1 1680
梦毁少年i
梦毁少年i 2020-12-18 17:59

I\'m using AngularFire 0.8

I called the following function to get an instance of an array.

var test = $firebase(new Firebase(URL)).$asArray();


        
相关标签:
1条回答
  • 2020-12-18 18:16

    Most likely you're calling console.log on the array before the data is loaded. To access the data in the array, you have to wait until the data is loaded.

    To do that in code, you use this construct:

    var test = $firebase(new Firebase(URL)).$asArray();
    test.$loaded().then(function(array) {
        console.log(array[0]);
    });
    

    Update

    This answer gets more upvotes than I think it's worth. When you're using AngularJS and AngularFire, it is often a bad omen when you use console.log to debug the data that is loaded.

    The AngularFire documentation on handling asynchronous operations now has to say on it:

    The easiest way to log the data is to print it within the view using Angular's json filter.

    {{ data | json }}

    AngularFire tells the Angular compiler when it has finished loading the data, so there is no need to worry about when it be available.

    0 讨论(0)
提交回复
热议问题