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