我正在尝试在离子项目中显示Web服务的结果,即JSON。我可以接收它并在控制台中显示,但不能在HTML视图中显示它。这是我的代码:
service.ts
searchPerson(admin: AdminInfo): Observable<Person[]> {
return this.http.post<Person[]>(this.personServiceApi, admin, httpOptions).pipe(
tap((newRequest: Person[]) => {
console.log(`fetched persons` + newRequest);}),
catchError(this.handleError<Person[]>('fetched persons'))
);
}
menupage.ts:
this.loginService.searchPerson(this.admin).subscribe((persons: Person []) => {
if (Array.isArray(persons)) { // checks if persons is an array
this.persons = persons;
} else {
console.error('persons in not an array');
console.log('Type of persons is : ' + typeof persons);
console.log('Persons:');
console.log(persons);
}
});
menupage.html
<ion-list>
<ion-item *ngFor="let person of persons">
<p>{{person.Address}}</p>
</ion-item>
</ion-list>
这是控制台中的结果:
fetched persons[object Object]
persons in not an array
Type of persons is : object
Persons:
{GetAllPersonResult: Array(41)}
GetAllPersonResult: Array(41)
0: {Address: "", Barcode: "", BirthDate: "",CertificateNo: "", Charge: ,
…}
1: {Address: "",Barcode: "", BirthDate: "",CertificateNo: "", Charge: ,
…}
2: {Address: "", Barcode: "", BirthDate: "", CertificateNo: "", Charge:
, …}
3: {Address: "", Barcode: "", BirthDate: "",CertificateNo: "", Charge: ,
…}
4: {Address: "", Barcode: "",BirthDate: "", CertificateNo: "", Charge: ,
…}
…………………………………………………………………………………………………………………………………………………………….
40: {Address: "", Barcode: "", BirthDate: "", CertificateNo: "", Charge:
, …}
4. length: 41
5. __proto__: Array(0)
2. __proto__: Object
答案 0 :(得分:0)
我认为罪魁祸首是O(NlogN)
不是sort(...)
。尝试使用此代码,并观察控制台中的错误
persons
答案 1 :(得分:-1)
您需要像这样更改ngOnInit方法:
this.loginService.searchPerson(this.admin).subscribe((persons: Person[]) => {
this.persons = persons;
console.log(this.persons);
});
让我知道这个答案是否有用