Я использую mat-select, и мне нужно установить для него значение по умолчанию, когда пользователь пытается обновить информацию. Я использую patchValue для обновления значений формы и установки значений по умолчанию, и он работает на других моих входах, кроме выбора mat.
Вот мой шаблон:
<form [formGroup]="profileForm" class="event-form w-100-p" fxLayout="column" fxFlex>
...
<mat-form-field appearance="outline" fxFlex="100" required>
<mat-label>Departement*</mat-label>
<mat-select formControlName="Département">
<mat-option *ngFor="let dept of departements" [value]="dept.id">
{{dept.name}}
</mat-option>
</mat-select>
<mat-icon matSuffix class="secondary-text">outlined_flag</mat-icon>
<mat-error>Selecter le departement</mat-error>
</mat-form-field>
А вот мой component.ts:
profileForm = this.fb.group({
Nom_de_famille: ['' ,Validators.required],
Prénom: ['' ,Validators.required],
email: ['', [Validators.email, Validators.required]],
Code_personnel: [Validators.compose([Validators.min(1000), Validators.max(9999)])],
//Validators.required
N_tel: [''],
birthday: [''],
adresse: [''],
lieu:[''],
CIN:['',Validators.compose([Validators.min(10000000), Validators.max(99999999)])],
Genre:[''],
N_permis:[''],
Département: ['',Validators.required]
});
ngOnInit() {
this.ListDepartmentEntity.forEach(e=>{
x++;
this.departements.push({id:x, name:e.Name}); // to fill departement options
});
this.setdefaultformvalues(this.indata.SendData);
}
setdefaultformvalues(row) {
let pos = 0, posDep = 0;
this.departements.forEach(e => {
pos++
if(row.DepartmentId==e.id){
posDep = pos; // to get current deparetement position
}
});
if(row.CIN==''){
row.CIN = null;
}
console.log(row.DepartmentString);
this.profileForm.patchValue({
Nom_de_famille: [row.FirstName],
Prénom: [row.LastName],
email: [row.Email],
Code_personnel: [row.DriverCode],
N_tel: [row.Tel],
birthday: null,
adresse: [row.Address],
lieu:[row.BirthPlace],
CIN:[row.CIN],
Genre:[row.DriverLicenseType],
N_permis:[row.DriverLicenseNumber],
Département: this.departements[posDep]
});
}
Rq: Я пробовал с [(value)] и ngModel раньше и ничего не происходит.