我如何安全地将值分配给Angular Reactive中的控件

问题描述 投票:0回答:1

我有一个Observable,它从API返回数据。在这种情况下,可以返回三种类型的地址:办公室,邮件和家庭。但是,如果用户未提供家庭住址,则该元素在API响应中不存在。

我使用

setInitialValue() {
this.userForm.setValue( {
  home_address_city = this.userProfile.home_address.city
})
}

只要该元素存在,就可以正常工作。因此,我想知道在没有this.userProfile.home_address.city的情况下处理此问题的最安全方法是什么

angular
1个回答
0
投票

使您的功能如下setInitialValue(){this.userForm.setValue({home_address_city = this.userProfile.home_address.city吗? this.userProfile.home_address.city:''})}

© www.soinside.com 2019 - 2024. All rights reserved.