如何使用 AngularFire2 获取 firebase 中元素的键?

How to get the key of an element in firebase with AngularFire2?

我在我的 Angular 项目中展示了 table 个产品,它显示得很好,但是我想要 [=24 格式的 link =] 不起作用。我得出的结论是 p.$key 没有显示密钥:

  <table class="table">
    <thead>
        <th>Title</th>
        <th>Price</th>
        <th></th>
    </thead> 
    <tbody>
        <tr *ngFor="let p of products$ | async">
            <td>{{ p.title }}</td>
            <td>{{ p.price }}</td>
            <td>
                <a [routerLink]="['/admin/products/', p.$key]">Edit</a>
            </td>
        </tr>
    </tbody>
</table>

p.$key 值未定义,有人知道如何解决吗?

Ps:如果有人需要知道products$的值,等于this.products$ = this.productService.getAll().valueChanges();

像这样在 valueChanges 中添加 idField 属性:

this.productService.getAll().valueChanges({idField: '$key'});

然后 firebase id 将在您的对象中可用,如下所示: p.$key

此外,考虑使用像 {{ p | json }} 这样的 json 管道进行开发,您可以在浏览器中看到对象中的所有值。