[英]Display Data from firebase with angularfire2
I use angular cli, realdatabase of firebase and angularfire2. 我使用angular cli,firebase的realdatabase和angularfire2。
I realize an application that records patients, lists them and gives access to each patient on a detail component. 我实现了一个可以记录患者,列出患者并在详细信息组件上访问每个患者的应用程序。 This component detail has for route: path: '/ patients /': id
该组件详细信息具有以下路线:路径:'/患者/':ID
Once on the detail component, I log diagnostics. 进入详细信息组件后,我将记录诊断信息。 The structure of my data is as follows:
我的数据结构如下:
I am now looking to display the diagnoses belonging to each patient in the detail component. 我现在希望在详细信息部分中显示属于每个患者的诊断。
I tried this but no data appears. 我试过了,但没有数据出现。 Thank you for your help
谢谢您的帮助
//diagnosticsService.ts : //diagnosticsService.ts:
import { Injectable } from '@angular/core';
import { Patient } from '../models/patient.model';
import { Diagnostic } from '../models/diagnostic.model';
import { AngularFireDatabase, AngularFireList, AngularFireObject} from 'angularfire2/database';
@Injectable()
export class DiagnosticsService {
diagnostics: AngularFireList<any>;
constructor(private database: AngularFireDatabase) {this.diagnostics = database.list('diagnostics');}
getDiagnostics() { return this.diagnostics; }
CreateDiagnostic(newDiagnostic: Diagnostic) { this.diagnostics.push(newDiagnostic); }
getDiagnosticByPatientid(Patientid: string){
return this.database.list('/diagnostics', ref => ref.orderByChild("Patientid").equalTo(Patientid)).valueChanges();
}
//ComponentDetail.ts : //ComponentDetail.ts:
import { Component, OnInit, Inject } from '@angular/core';
import { Patient } from '../../models/patient.model';
import { Diagnostic } from '../../models/diagnostic.model';
import { ActivatedRoute, Router } from '@angular/router';
import { PatientsService } from '../../services/patients.service';
import { DiagnosticsService } from '../../services/diagnostics.service';
import { AngularFireDatabase, AngularFireList, AngularFireObject, AngularFireAction } from 'angularfire2/database';
import { Location } from '@angular/common';
import { Observable } from 'rxjs/Observable';
@Component({
selector: 'app-single-patient',
templateUrl: './single-patient.component.html',
styleUrls: ['./single-patient.component.scss'],
providers: [PatientsService]
})
export class SinglePatientComponent implements OnInit {
patientId: string;
diagnosticToDisplay;
constructor(
private route: ActivatedRoute,
private location: Location,
private patientsService: PatientsService,
private diagnosticsService: DiagnosticsService,
private router: Router,
) {}
ngOnInit() {
this.route.params.forEach((urlParameters) => {
this.patientId = urlParameters['id'];
this.diagnosticToDisplay =
this.diagnosticsService.getDiagnosticByPatientid(this.patientId);
console.log(this.diagnosticToDisplay);
}
//DetailComponent.html : //DetailComponent.html:
<li *ngFor="let diagnosticToDisplay of diagnosticsToDisplay | async ">
{{ diagnosticToDisplay?.localisation }}
</li>
It looks to me like you need to unwrap the observable of diagnosticsToDisplay. 在我看来,您需要打开可观察到的diagnosticsToDisplay。 Remember that angularfire will return an observable that is a list.
请记住,angularfire将返回一个可观察的列表。 NgFor only works on lists, and so I'm surprised you're not getting an error from trying to use an observable instead of a list.
NgFor仅适用于列表,因此,令您惊讶的是,尝试使用可观察的对象而不是列表不会出现错误。 With this idea in mind your html instead should read
考虑到这个想法,您的html应该改为
<li *ngFor="let diagnosticToDisplay of diagnosticsToDisplay | async ">
{{ diagnosticToDisplay?.localisation }</li>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.