【发布时间】:2019-05-28 03:08:48
【问题描述】:
我有一个 HTML 页面,我在其中显示患者列表。 (从服务调用调用到 json 服务器的列表)。当用户点击一个病人时,它应该显示页面的详细信息。
在服务中
getPatientList(): Observable<any> {
return this.http.get<any[]>(`${endpoint}/patient-details`);
}
getPatientDetails(id: number): Observable<any> {
return this.http.get<any[]>(`${endpoint}/patient-details?
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.route.params.subscribe(params => {
this.pId=params.profile_no;
});
console.log(this.pId);
this.data.getPatientDetails(this.pId).subscribe(
patient => {
console.log(patient);
this.patient = patient;
console.log(this.patient);
});
}
console.log(patient) 总是显示一个空数组。
<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>
【问题讨论】:
-
如果您没有使用
any作为所有变量和返回类型的类型,而是使用实际类型,编译器将能够告诉您哪里出了问题。这就是我们使用 TypeScript 的原因。定义类型并使用它们。尽可能避免any。根据您发布的内容,记录的值应该是未定义的。它不可能是 Observable。使用类型,然后发布你的真实代码和你的真实输出。 -
我无法设置固定类型的患者对象,因为根据患者提供的数据,每个患者可能有不同的字段。
-
您能否
curl网络服务并得到适当的响应? -
你能指导我怎么做吗?