When iterating through a list of items in AngularFire, how do I get the ID of each item? [duplicate]
-
21-12-2019 - |
Pergunta
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?
Solução
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>
Outras dicas
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();
});
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow