Aamir shah
Aamir shah

Reputation: 151

angularFire get $id from nested object

i have a situation where i have to put nested ng-repeat, when i am trying to get the $id of first ng-repeat i got it without any problem, but when i tried to get the $id of second ng-repeat (marked red in picture below) i got undefined.

my Firebase structure is as follows

enter image description here

here is items object items object

HTML Code and Controller are here

angular
  .module('starter')
  .controller('feedController', function($scope, $firebaseRef, $firebaseArray) {

    $scope.allRequests = $firebaseArray($firebaseRef.requests);

    $scope.allRequests.$loaded(function() {
      console.log(arguments);
    })

  })
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<span ng-repeat="items in allRequests">   
    <!-- this works fine -->
      {{items.$id}}
      
      <div ng-repeat="item in items">
        
       <!-- getting undefined here... -->
      {{item.$id}}
      
    </div>
 </span>

i need $idof second ng-repeat, any help will be appreciated.

Upvotes: 2

Views: 553

Answers (1)

VonD
VonD

Reputation: 5155

You can use the (key, value) syntax to iterate over your items :

<div ng-repeat="(id, item) in items">
  {{id}}
  {{item.userName}}
</div>

What happens is that the $id and $priority are injected by angularfire on the node on which you made the request. It doesn't inject it in all the sub-nodes of this node, so your individual items do not have a $id property.

When you call ng-repeat="item in items", as items is an object, angular will iterate over your object's properties, and silently ignore the ones starting with $, because this prefix is often used internally by angular. This is a side-effect that angularfire takes advantage of (otherwise, in your item in items loop, you would iterate over $id and $priority). The (key, value) in object syntax gives you access to your child object's key.

Upvotes: 7

Related Questions