2

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.

7
  • can you show an example of what items look like ? Commented Apr 14, 2016 at 13:38
  • @VonD i updated my question, please see items Commented Apr 14, 2016 at 13:43
  • a little offtopic advice: you should not use block elements like div inside inline elements like span Commented Apr 14, 2016 at 13:49
  • @Lulylulu thanks, i'll look into it, but my first priority is to solve this issue. Commented Apr 14, 2016 at 13:52
  • @Aamirshah what you printed is the items object or you allRequests object ? Are you looking for the id 0347a3c0-.. or the KFJek... ? Commented Apr 14, 2016 at 14:02

1 Answer 1

7

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Always wondered why this works. Thanks for the great explanation!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.