简体   繁体   English

Angular 反应形式补丁值不适用于 mat-select 下拉菜单

[英]Angular reactive form patchvalue not working with mat-select dropdown

I am building reactive form in which I have a mat-select dropdown with two button submit and load data.我正在构建反应式表单,其中我有一个带有两个按钮提交和加载数据的 mat-select 下拉列表。 If user click on load data button I have to set a value to drop down I am using reactive forms patchValue() method to achieve this but the value the not reflected in option.如果用户单击加载数据按钮,我必须设置一个下拉值我正在使用反应式 forms patchValue() 方法来实现此目的,但该值未反映在选项中。 I have created the same scenario under this link `我在此链接下创建了相同的场景`

https://stackblitz.com/edit/angular-select-dropdown-reactive-form-yhevca?file=src%2Fapp%2Fapp.component.ts https://stackblitz.com/edit/angular-select-dropdown-reactive-form-yhevca?file=src%2Fapp%2Fapp.component.ts

` `

could anyone please tell me how can I achieve this谁能告诉我我怎样才能做到这一点

You are patching cityName: "srinagar" this value doesn't exist in the cities options list, try to use one of the options values eg "Florida" and you will see the patched value correctly bindded您正在修补cityName: "srinagar"这个值在城市选项列表中不存在,尝试使用选项值之一,例如 "Florida",您将看到正确绑定的修补值

Try adding this in your loadData()尝试将其添加到您的 loadData()

this.registrationForm.controls["cityName"].setValue(this.City[0]);

Here I'm setting the value of 0th index of your array.在这里,我正在设置数组的第 0 个索引的值。 You can set any value you want.你可以设置任何你想要的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM