How do you access loaded data from .$on('loaded') in AngularFire 0.5.0

前端 未结 2 1641
独厮守ぢ
独厮守ぢ 2020-12-29 16:36

I am having trouble accessing the data loaded from firebase using the .$on method in AngularFire 0.5.0

In the callback when I log out the contents of the scope the d

相关标签:
2条回答
  • 2020-12-29 17:18

    Based on Kato's answer here I have been able to solve this.
    I was unaware the loaded event passed the raw data of the loaded asset, it is undocumented in the AngularFire docs.

    It doesn't explain the odd behaviour I was having with console.log but it does solve the problem.

    .controller('AssetDetailCtrl',
      ['$scope', '$firebase', 'FBURL',
      function($scope, $firebase, FBURL) {
        var refAsset = new Firebase(FBURL + '/assets/' + $scope.assetId);
        $scope.asset = $firebase(refAsset);
    
        // when data is loaded check validity of the route
        $scope.asset.$on('loaded', function(value) {
          console.log(value); // data loaded from Firebase
          console.log(value.name); // subset of the returned value
        });
    }])
    
    0 讨论(0)
  • 2020-12-29 17:30

    Just for my own sake and others out there that may be having the same issue as i did, stumbling upon this question googling for the answer. My problem was that sometimes i was getting undefined as the value from the callback of $on('loaded').

    The way i solved it in the end was to use that event as a way of knowing when i could call the resource.$getIndex(), and the use the indexes returned from that to loop through the content and possibly store that in a new array to access the raw data.

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