angular - Angular:无法读取未定义的属性但仍显示数据
问题描述
我很困惑,因为我的应用程序正在显示必要的数据,但控制台中仍然存在我想摆脱的错误。
打字稿:
activeClient: Client;
constructor(private clientService: ClientService) { }
ngOnInit() {
this.inViewMode = true;
this.clientId = parseInt(window.location.pathname.split('/')[2], 10);
this.clientService.getClientById(this.clientId)
.subscribe(data => this.activeClient = data);
}
HTML:
<div class="form-row">
<div class="form-group col-12">
<label for="name">Ettevõtte nimi<span *ngIf="!inViewMode" class="required">*</span></label>
<input class="form-control" id="name" [value]="activeClient.companyName">
</div>
</div>
客户型号:
export interface Client {
id?: number;
companyName?: String;
firmRegNo?: number;
address?: String;
clientName?: String;
phoneOne?: number;
phoneTwo?: number;
email?: String;
explanation?: String;
rating?: number;
status?: String;
clientContract?: ClientContract;
}
解决方案
使用安全导航运算符来防止错误,直到activeClient
被初始化:
[value]="activeClient?.companyName"
推荐阅读
- pandas - Pandas cut() 创建更少的垃圾箱
- php - 如何制作正则表达式来检测rupay借记卡
- android - 如何在android的textinputlayout中更改FloatingPlaceholderColor但不提示颜色
- java - 如何从 Firebase 检索数据并将其设置为图表的字符串和整数
- c# - 将 UInt32 数组转换为图像/位图
- python - 一次热编码后数据帧中的行数减少
- python - 遍历切片列表并匹配具有 dict 值的元素
- ggplot2 - 如何解决此错误:二进制运算符的非数字参数
- mysql - MySQL 查询 WHERE
- reactjs - 如何对循环返回的jsx的元素进行换行?