angular制作一个根据患者Id查询患者的方法,患者数据写不到页面上。
患者数据是用数组储存的。
```typescript
const Patient: any[]= [
{ Id: "0", FirstName: "Stark", LastName: "Bill", DateOfBirth: "2000/10/11", Gender: "Male", PrimaryInsurance: "Medicare", Address: "ddadsdasd", ContactNumber: "1338872818738", NextOfKin: "weqe" },
{ Id: "1", FirstName: "Shown", LastName: "Jenny", DateOfBirth: "2000/10/11", Gender: "Male", PrimaryInsurance: "Medicare", Address: "ddadsdasd", ContactNumber: "1338872818738", NextOfKin: "weqe" },
{ Id: "2", FirstName: "Geogrge", LastName: "Hash", DateOfBirth: "2000/10/11", Gender: "Male", PrimaryInsurance: "Medicare", Address: "ddadsdasd", ContactNumber: "1338872818738", NextOfKin: "weqe" },
{ Id: "3", FirstName: "Intwre", LastName: "zaiwoo", DateOfBirth: "2000/10/11", Gender: "Male", PrimaryInsurance: "Medicare", Address: "ddadsdasd", ContactNumber: "1338872818738", NextOfKin: "weqe" },
{ Id: "4", FirstName: "Niko", LastName: "baby", DateOfBirth: "2000/10/11", Gender: "Male", PrimaryInsurance: "Medicare", Address: "ddadsdasd", ContactNumber: "1338872818738", NextOfKin: "weqe" }
];
const result: any[]= [];
我做的搜索方法:
```typescript
search(Id: string){
const index = Patient.indexOf(Id);
var item = Patient [index];
const result = Patient.filter(function (item) {
if (item.Id== Id) {
return true;
} else {
return false;
}
})
console.log(result);
}
html:
Search a patient by ID
<label for="Patient_IDText3">Enter patient_ID: </label>
<input type="text" id="patient_IDText3" [(ngModel)]="patient_IDText3" [ngModelOptions]="{standalone: true}"/>
<input type="button" value="Search" (click)="search(patient_IDText3)">
在页面输入ID在控制台是可以看见搜到数据的:
可当我尝试新做一个表格来显示这个搜到的数据时它报错。
我尝试的代码:
<table class="list-table">
<thead>
<th>Patient ID*</th>
<th>First name*</th>
<th>Last name*</th>
<th>Date of birth *</th>
<th>Gender*</th>
<th>Primary insurance*</th>
<th>Address *</th>
<th>Contact number *</th>
<th>Next of kin</th>
</thead>
<tbody>
<tr *ngFor="let result of result">
<td>{{ result.Id }}</td>
<td>{{ result.FirstName }}</td>
<td>{{ result.LastName }}</td>
<td>{{ result.DateOfBirth }}</td>
<td>{{ result.Gender }}</td>
<td>{{ result.PrimaryInsurance }}</td>
<td>{{ result.Address }}</td>
<td>{{ result.ContactNumber }}</td>
<td>{{ result.NextOfKin }}</td>
</tr>
</tbody>
</table>
错误:
于是我尝试在AppComponent里声明了一下result:
result=result;
它报这个错,于是我加了this.
它报这个错。
于是我尝试新建一个空数组result,html表格里绑定result数组里的数据,当使用search方法时将filter出来的数据加到result数组里。
search(Id: string){
const index = Patient.indexOf(Id);
var item = Patient [index];
const re = Patient.filter(function (item) {
if (item.Id== Id) {
return true;
} else {
return false;
}
})
console.log(re);
result.push({ Id: this.re.Id,
FirstName:this.re. FirstName,
LastName: this.re.LastName,
DateOfBirth: this.re.DateOfBirth,
Gender: this.re.Gender,
PrimaryInsurance:this.re.PrimaryInsurance,
Address:this.re.Address,
NextOfKin:this.re.NextOfKin
});
}
但它又报这个错
问题好像又回到原点了。
请问该怎么解决这个问题让网页可以显示搜索出来的数据?