When iterating through a list of items in AngularFire, how do I get the ID of each item? [duplicate]

StackOverflow https://stackoverflow.com//questions/23026594

質問

Controller:

...

$scope.items = ItemService; // This is injected correctly, no problem here.

...

HTML:

<div ng-repeat="item in items">
  <a ng-href="#/items/{{ item.id }}">{{ item.name }}</a>
</div>

My issue is... if I add items using .$add() (Firebase's .push()), I don't know how to get the auto-generated ID...

Any ideas?

役に立ちましたか?

解決

Turns out you can iterate over (key, value), which does the trick:

HTML:

<div ng-repeat="(id, item) in items">
  <a ng-href="#/items/{{ id }}">{{ item.name }}</a>
</div>

他のヒント

In addition to iterating over key/value, each record in $firebase (assuming it's not a primitive value) will have the key attached as $id:

<div ng-repeat="item in items">
  <a ng-href="#/items/{{ item.$id }}">{{ item.name }}</a>
</div>

This question is answered here: https://stackoverflow.com/a/21996544/975669

the answer:

The angularFire $add method returns a promise, which when resolved will contain a Firebase reference to your newly pushed value. With that value, you can get the UID.

$scope.tests.$add({
  ...your object....
}).then(function(ref) { 

    //this is your id
    var id = ref.name(); 
  });
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top