我正在从返回JSON对象的web API获取数据,它返回正常,但当我循环遍历它时,它抛出一个错误,无法读取未定义的'CustomerName‘的属性。
[System.Web.Http.HttpGet]
public object GetBookings(string SearchByParam = "", byte WhichRecords
= 1)
{
List<ZahidCarWash.ViewModels.BookingArrivalCancellationViewModel>
lstBookingArrivalCancellation =
BookingsRepository.SelectCustomerBookingDetailsAndroid(SearchByParam,
WhichRecords);
if (lstBookingArrivalCancellation.Count > 0)
{
return Json(new { ReturnStatusJSON = true, lstArrivalCancellation = lstBookingArrivalCancellation });
}
else
{
return Json(new { ReturnStatusJSON = false, lstArrivalCancellation = lstBookingArrivalCancellation });
}
}Customer.service.ts
export class CustomerService {
formData: Customer;
Data: Customer[];
constructor(private http: HttpClient) { }
getData() {
return
this.http.get('http://localhost:13924/api/AndroidOperations/GetBookings')
.subscribe(function(data: any) {
this.Data = data['lstArrivalCancellation'] as Customer[];
console.log(this.Data);
});
}
}customer-list.component.ts
export class CustomersListComponent implements OnInit {
constructor(public service: CustomerService) {
}
ngOnInit() {
this.service.getData();
}
}.html
<table class="table table-hover">
<tr *ngFor="let d of service.Data | async"></tr>
<td *ngIf="CustomerName">{{d.CustomerName}}</td>
<td>Tr</td>
</table>客户模式:
export class Customer {
CustomerID: number ;
CustomerName: string;
ContactNo: string;
VehicleRegNo: string;
fk_VehicleMakeID: number;
VehicleModel: number;
}发布于 2018-12-22 20:01:32
尝试将html代码中的ngIf从
<td *ngIf="CustomerName">{{d.CustomerName}}</td>至
<td *ngIf="d">{{d?.CustomerName}}</td>发布于 2018-12-23 01:44:04
您在应该检查d.CustomerName的*ngIf语句中检查CustomerName。D‘会一直存在,因为你要遍历它所在的集合。但是d.CustomerName是未定义的,它是rails。
<div *ngIf="d.CustomerName">{{d.CustomerName}}</div应该能行得通。
https://stackoverflow.com/questions/53895430
复制相似问题