AngularFire2 - 在$ key

时间:2016-09-14 17:15:49

标签: angular angularfire2

我正在尝试编写一个查询,根据它的密钥从集合中提取单个项目。

我一直在搜索和关注很多教程,但似乎只是展示如何获得如下列表:

我想传递$ key和查询并​​拉出一条记录。任何可能有帮助的来源的建议或指示都将受到赞赏。

import { Injectable } from '@angular/core';

import { Customer } from "./customer";

import { AngularFire, FirebaseListObservable} from 'angularfire2';

@Injectable()
export class CustomerService {

    customer: Customer;
    customers: FirebaseListObservable<Customer[]>;
    categories: FirebaseListObservable<Category[]>;

    constructor(private af: AngularFire) { }

    getCustomer(customerIndex: number) {

        this.customers = this.af.database.list('customer');

        return this.customers;
    }
}

1 个答案:

答案 0 :(得分:3)

如果你知道密钥,你可以这样做:

this.af.database.object('/customers/' + key)
  .subscribe(customer =>{
     // Assuming that name is a value of customer you can say
     var name = customer.name; 
     ...
  }

当然,这是假设&#34;客户&#34;是您之前推送过的客户列表。