当遍历AngularFire中的项目列表时,如何获得每个项的ID ?(复制)

时间:2021-04-03 22:34:18

This question already has an answer here:

这个问题已经有了答案:

Controller:

控制器:

...

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

...

HTML:

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...

我的问题是…如果我添加了使用的项。$add() (Firebase的。push()),我不知道如何获得自动生成的ID…

Any ideas?

什么好主意吗?

3 个解决方案

#1


1  

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

结果是,你可以迭代(key, value),这是一个技巧:

HTML:

HTML:

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

#2


1  

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:

除了迭代键/值之外,每个$firebase的记录(假设它不是一个原始值)将有一个作为$id的键:

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

#3


0  

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

这个问题的答案是:https://*.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.

angularFire $add方法返回一个承诺,当解析将包含一个Firebase引用到您的新推送的值。有了这个值,就可以得到UID。

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

    //this is your id
    var id = ref.name(); 
  });

#1


1  

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

结果是,你可以迭代(key, value),这是一个技巧:

HTML:

HTML:

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

#2


1  

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:

除了迭代键/值之外,每个$firebase的记录(假设它不是一个原始值)将有一个作为$id的键:

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

#3


0  

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

这个问题的答案是:https://*.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.

angularFire $add方法返回一个承诺,当解析将包含一个Firebase引用到您的新推送的值。有了这个值,就可以得到UID。

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

    //this is your id
    var id = ref.name(); 
  });