简体   繁体   English

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

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

I am displaying a table of products in my Angular project, it displays just fine but the link which I want in the format /admin/products/key doesnt work.我在我的 Angular 项目中显示产品表,它显示得很好,但我想要的/admin/products/key格式的链接不起作用。 I have come to the conclussion that the p.$key isnt showing the key:我得出的结论是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 value is undefined, does someone knows how to fix it? p.$key值未定义,有人知道如何解决吗?

Ps: If anyone needs to know the value of products$ , it is equal to this.products$ = this.productService.getAll().valueChanges(); ps:如果有人需要知道products$的值,等于this.products$ = this.productService.getAll().valueChanges();

Add the idField property inside of valueChanges like this:在 valueChanges 中添加 idField 属性,如下所示:

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

Then the firebase id will be available within your object like this: p.$key然后 firebase id 将在您的 object 中可用,如下所示: p.$key

Also, consider using the json pipe like {{ p | json }}另外,考虑使用 json pipe 像{{ p | json }} {{ p | json }} for development and you can see all of the values in the object in your browser. {{ p | json }}用于开发,您可以在浏览器中查看 object 中的所有值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM