【发布时间】:2019-05-26 09:40:36
【问题描述】:
我有一个 HTML 页面,我在其中显示患者列表。 (从服务调用调用到 json 服务器的列表)。当用户点击一个病人时,它应该显示页面的详细信息。
在服务中
getPatientList(): Observable<any> {
return this.http.get<any[]>(endpoint).pipe(
tap(data => console.log( JSON.stringify(data)))
);
}
getPatientDetails(id: number): Observable<any> {
return this.getPatientList().pipe(
map((patient: any[]) => patient.find(p => p.profile_no === id))
);
}
患者详细信息.ts
import { Component, OnInit, Input } from '@angular/core';
import { DataService } from '../services/data.service';
import { Router, ActivatedRoute } from '@angular/router';
@Component({
selector: 'app-patient-details',
templateUrl: './patient-details.component.html',
styleUrls: ['./patient-details.component.css']
})
export class PatientDetailsComponent implements OnInit {
patient : any;
pId: number;
constructor(private data: DataService, private route: ActivatedRoute) { }
ngOnInit() {
this.data.getPatientDetails(this.pId).subscribe(
patient => this.patient = patient);
console.log(this.patient);
}
}
console.log(patient) display following in console.
Observable {_isScalar: false, source: Observable, operator: MapOperator}
这是HTML模板的一部分
<div class="form-group child">
<h1>{{patient.first_name}} {{patient.last_name}}</h1>
<div class="col form-group form-control-md mt-5">
<label for="profileNo" style="width:25%">Profile No: </label>
<label></label>
<input type="text" class="input-group-sm" [(ngModel)]="patient.profile_no">
</div>
<div class="col form-group form-control-md">
<label for="DOB" style="width:25%">Date of Birth: </label>
<input type="text" class="input-group-sm" [(ngModel)]="patient.DOB">
</div>
<div class="col form-group">
<label for="gender" style="width:25%">Gender: </label>
<input type="text" class="input-group-sm"[(ngModel)]="patient.gender">
</div>
<div class="col form-group">
<label for="bloodGroup" style="width:25%">Blood Group: </label>
<input type="text" class="input-group-sm" [(ngModel)]="patient.blood_group">
</div>
【问题讨论】:
-
解释你的问题
-
请在您绑定的地方发布 HTML 模板,并发布 .TS 类。
-
用html模板和ts编辑
-
请问您的
Id是如何注入endpoint的?意味着如果您尝试使用本地消费者(例如邮递员)测试您的服务,如何通过id? :endpoint/id,endpoint?id=id... -
我已经运行了 json 服务器。在 json 服务器上,当我运行以下命令时,它为我提供了患者的完整详细信息。 localhost:3000/patient-details?profile_no=1
标签: angular