[英]Reactive forms patchValue() with ngValue not working
I have one dropdown in reactive forms I have used the whole object as the value( {NAME:'test',CODE:'002'}
) for further processing, its working fine I am able to get selected item in .ts file.In edit mode, I have to bind selected item to drown using patchValue but it's not working.我有一个反应形式的下拉列表,我使用整个对象作为值(
{NAME:'test',CODE:'002'}
)进行进一步处理,它工作正常,我能够在 .ts 文件中获取所选项目。在编辑模式下,我必须使用 patchValue 将所选项目绑定到淹没,但它不起作用。
html html
<select class="form-group form-control" formControlName="county">
<option value="">-Select-</option>
<option *ngFor="let item of countyDataset" [ngValue]='item'>{{item.NAME}}
</option>
</select>
.ts code .ts 代码
bindDataValue(){
const rec=county{
CODE: "028"
NAME: "LOS ALAMOS"
}
this.form.patchValue(rec);
}
<h1>Reactive Forms</h1>
<div style="padding: 0 0 20px 20px">
<h3>Value string</h3>
<form [formGroup]="selectForm">
<mat-form-field>
<mat-select name="countryReactiveString" formControlName="selectedCountryControl" placeholder="Country">
<mat-option *ngFor="let country of countries" [value]="country">{{country?.full}}</mat-option>
</mat-select>
</mat-form-field>
</form>
{{selectForm.value | json}}
</div>
import { Component, OnInit } from '@angular/core';
import { FormControl, FormGroup, FormBuilder } from '@angular/forms';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.scss' ]
})
export class AppComponent implements OnInit {
selectForm: FormGroup;
constructor(private fb: FormBuilder) {}
countries: any = [
{
full: "Great Britain",
short: "GB"
},
{
full: "United States",
short: "US"
},
{
full: "Canada",
short: "CA"
}
];
ngOnInit() {
this.selectForm = this.fb.group({
selectedCountryControl: []
})
this.selectForm.patchValue({
selectedCountryControl: this.countries[0]
})
}
}
Here is demo it should workhttps://stackblitz.com/edit/angular-material-select-demo-d7edt6这是它应该工作的演示https://stackblitz.com/edit/angular-material-select-demo-d7edt6
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.