Угловая база не показывает Ключ - PullRequest
0 голосов
/ 31 мая 2018

Я хочу показать $ key клиента из firebasedatabase, который также называется uniquekey, может быть, как

-Kdl_wRRkn7njxgz4B54

я пытаюсь, но он не показывает ключ, но также показывает другие данные в ключепопробуйте заменить ключ $ не работает ключ.я знаю, что нужно изменить код, если кто-то может поблагодарить :)

client.html

    <div class="row">
  <div class="col-md-6">
    <h2><i class="fa fa-users"></i> Clients</h2>
  </div>
  <div class="col-md-6">
    <h5 class="pull-right text-muted">Total Owed: {{totalOwed | currency:"USD":true}}</h5>
  </div>
</div>
<table *ngIf="clients?.length > 0;else noClients" class="table table-striped">
  <thead class="thead-inverse">
    <tr>
      <th>ID</th>
      <th>Name</th>
      <th>Email</th>
      <th>Balance</th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr *ngFor="let client of clients">
      <td>{{client.$key}}</td>
      <td>{{client.firstName}} {{client.lastName}}</td>
      <td>{{client.email}}</td>
      <td>{{client.balance | currency:"USD":true}}</td>
      <td><a [routerLink]="['/client/'+client.$key]" href="" class="btn btn-secondary btn-sm"><i class="fa fa-arrow-circle-o-right"></i> Details</a></td>
    </tr>
  </tbody>
</table>

<ng-template #noClients>
  <hr>
  <h5>There are no clients in the system</h5>
</ng-template>

client.ts

import { Component, OnInit } from '@angular/core';
import { ClientService } from '../../services/client.service';
import { Client } from '../../models/Client';

@Component({
  selector: 'app-clients',
  templateUrl: './clients.component.html',
  styleUrls: ['./clients.component.css']
})
export class ClientsComponent implements OnInit {
clients:any[];
  totalOwed:number;

  constructor(
    public clientService:ClientService
  ) { }

  ngOnInit() {
    this.clientService.getClients().valueChanges().subscribe(clients => {
      this.clients = clients;
      this.getTotalOwed();
    });
  }

    getTotalOwed(){
    let total = 0;
    for(let i = 0;i < this.clients.length;i++){
      total += parseFloat(this.clients[i].balance);
    }
    this.totalOwed = total;
    console.log(this.totalOwed);
  }

}

client.service.ts

import { Injectable } from '@angular/core';
import { AngularFireDatabase} from 'angularfire2/database';
import { AngularFireObject, AngularFireList } from 'angularfire2/database';
import { Observable } from 'rxjs';
import { Client } from '../models/Client';

@Injectable()
export class ClientService {
  clients: AngularFireList<any>;
  client: AngularFireObject<any>;

  constructor(
    public af:AngularFireDatabase
  ) { 
    this.clients = this.af.list('/clients') as AngularFireList<Client[]>;
  }

  getClients(){
    return this.clients;
  }

  newClient(client:Client){
    this.clients.push(client);
  }

  getClient(id:string){
    this.client = this.af.object('/clients/'+id) as AngularFireObject<Client>;
    return this.client;
  }

}

1 Ответ

0 голосов
/ 31 мая 2018

Используйте snapshotChanges().map() для хранения ключа:

constructor(
  public af:AngularFireDatabase
) { 
  this.clientsRef = this.af.list('/clients') as AngularFireList<Client[]>;
  this.clients = this.clientsRef.snapshotChanges().pipe(
    map(changes => 
      changes.map(c => ({ key: c.payload.key, ...c.payload.val() }))
    )
  );  
}

Тогда вы сможете получить к нему доступ как обычно:

<td>{{client.key}}</td>
...