c# - 为什么我的角度代码抛出错误:无法读取未定义的属性?
问题描述
我从返回 JSON 对象的 Web API 获取数据,它返回正常,但是当我循环它时,它会抛出一个错误,即无法读取未定义的“客户名称”的属性。
[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 });
}
}
客户服务.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);
});
}
}
客户列表.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;
}
解决方案
您正在检查 *ngIf 语句中的 CustomerName,您应该检查 d.CustomerName。'd' 将始终存在,因为您正在迭代它所在的集合。但是 d.CustomerName 的未定义它是轨道。
<div *ngIf="d.CustomerName">{{d.CustomerName}}</div
应该做的伎俩。
推荐阅读
- html - 背景图像未从互联网加载
- highcharts - Highcharts如何将数据标签添加到系列的最后一个数据点
- php - 我在使用 PHP(一组十组,每组四个单选按钮)进行的测验中获取信息以提交到 PHP 测试结果页面时遇到问题
- sql - 有没有办法在 SQL 中按列名对两个日期列进行分类?
- spartacus-storefront - 从购物车移动到结帐时,第一次客人结帐时出现未找到的 API
- mysql - 仅显示价格满足条件的行(与最近的价格行相比)
- java - 一个线程中的 StackOverFlow 异常会导致应用程序停止吗?
- c# - 如何使网格固定,不受滚动影响?
- ios - 参数:[字符串:任意]
- django - 如何在没有外键列的情况下从 django 的管理员端导入 csv 文件