gpt4 book ai didi

javascript - Angular - 在对象的新属性中创建属性

转载 作者:太空狗 更新时间:2023-10-29 17:56:13 25 4
gpt4 key购买 nike

目前,我的 html 中有一个 select 元素,它有一个对象 details 的 ngModel:

[ngModel]="details?.publicInformation?.firstname" 

但是,publicInformation 可能不存在于该对象中,或者即使存在,也可能 firstname 不存在。不管怎样,最后,我想创建以下内容:

[ngModel]="details?.publicInformation?.firstname" (ngModelChange)="details['publicInformation']['firstname'] = $event"

基本上,如果选择被触发,即使 publicInformationfirstname 都不存在,我想在 details 和存储来自选择的值。

问题是我得到了

Cannot set property 'firstname' of undefined

谁能解释一下我在这里做错了什么,我怎样才能达到我想要的结果?

最佳答案

需要将detailspublicInformation初始化为空对象

public details = {publicInformation : {}};

关于javascript - Angular - 在对象的新属性中创建属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52030144/

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