angular - PrimeNG 自动完成更改表单数据模型
问题描述
我在 PrimeNG 7.0.0 和 Autocomplete 组件中遇到了一些奇怪的行为。此问题的摘要是,无论我的源JSON对象的结构是什么结构,它都会在选择项目时用此结构覆盖我的FormGroup模型。
我的 FormGroup 对象看起来像:
exportCountry: this.fb.group({
id: [Validators.required, Validators.maxLength(2)] // 5/14 a2 UPPERCASE
}),
这会产生以下 JSON:
{
"exportCountry": {
"id": ""
},
}
我的建议源 JSON 如下所示:
{"countries":
[
{
"code": "FR",
"name": "FRANCE"
},
{
"code": "AD",
"name": "ANDORRA"
},
{
"code": "AE",
"name": "UNITED ARAB EMIRATES"
},.....................
]
}
我有一个包含 autoComplete 组件的组件:
<div formGroupName="exportCountry">
<p-autoComplete [suggestions]="filteredCountriesSingle" [inputId]="type+'countryId'"
(completeMethod)="filterCountrySingle($event)" field="code"
[minLength]="1" [inputStyleClass]="'form-control'"
formControlName="id">
<ng-template let-country pTemplate="item">
<div class="ui-helper-clearfix" style="width: 20em">
<span>{{country.code}}</span>
<span> - </span>
<span>{{country.name}}</span>
</div>
</ng-template>
</p-autoComplete>
</div>
.ts 文件基本上遵循 PrimeNG 展示上的示例,所有这些都正确显示并过滤并显示正确的建议。但是,一旦从选项中选择了一个项目,我的 FormGroup 对象/JSON 将更改为:
"exportCountry": {
"id": {
"code": "FR",
"name": "FRANCE"
}
},
我要求根据最初分配的 FormControl 设置所选代码,即
{
"exportCountry": {
"id": "FR"
},
}
非常感谢任何帮助。
解决方案
推荐阅读
- jenkins - Jenkins podTemplate 禁用默认回声
- python - 使用 Xpath 获取在属性之一中包含特定单词的 XML 节点
- angularjs - 如何在 angularJS 的 pdf 页面的顶部和底部添加边距?
- ios - 如何在 Swift AppDelegate 之外获取 Firebase FCM 令牌
- html - 为什么在关闭 div 标签后应用 hr 后 hr 与 div 元素在同一行
- python - 将不包含表格的 HTML 转换为 pandas Dataframe
- excel - 如何在状态栏中为 Microsoft Excel 365 MSO 64 位中的总和添加逗号分隔符?
- c++ - Qt 添加 libQGLViewer
- grep - 如何使用 grep 删除多列(仅第一列)?
- python - 在 Python 中查找两个字符之间的字符