gpt4 book ai didi

Angular:无法读取未定义的属性但仍显示数据

转载 作者:行者123 更新时间:2023-12-02 00:23:50 26 4
gpt4 key购买 nike

我很困惑,因为我的应用程序正在显示必要的数据,但控制台中仍然存在我想消除的错误。

typescript :

  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;
}

最佳答案

使用 safe navigation operator在初始化 activeClient 之前防止错误:

[value]="activeClient?.companyName"

关于Angular:无法读取未定义的属性但仍显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54536126/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com