[英]How can I set default selected value to mat-select in Angular reactive form?
I'm setting a default value to a drop-down with reactive forms.我正在为带有反应形式的下拉列表设置默认值。 I did this
我做了这个
HTML HTML
<form [formGroup]="estadosForm" (ngSubmit)="aplicarCambios()">
<div class="campo campo-x2">
<mat-form-field>
<mat-label>Descripcion</mat-label>
<input matInput formControlName="descripcionForm">
</mat-form-field>
</div>
<div class="campo campo-x2">
<mat-form-field>
<mat-label>Clase de estado</mat-label>
<mat-select formControlName="clasesDeEstadoForm" (selectionChange)="onChangeClaseDeEstado($event.source.value)">
<mat-option *ngFor="let claseDeEstado of clasesDeEstado" [value]="claseDeEstado">
{{claseDeEstado.descripcion}}
</mat-option>
</mat-select>
</mat-form-field>
</form>
Typescript打字稿
this.f.descripcionForm.setValue(estado.descripcion);
this.f.clasesDeEstadoForm.setValue(estado.idClaseEstado, { onlySelf: true });
This sets the value to the form, but is not shown as the default value en el HTML, I tried placing the [(value)]
property in mat-select
tag but it didn't work这将值设置为表单,但未显示为默认值 en el HTML,我尝试将
[(value)]
属性放置在mat-select
标记中,但它不起作用
first change value of mat-option to claseDeEstado's id.首先将 mat-option 的值更改为 claseDeEstado 的 id。 for example
例如
<mat-option *ngFor="let claseDeEstado of clasesDeEstado" [value]="claseDeEstado.id">
{{claseDeEstado.descripcion}}
</mat-option>
then set selected Value's id.然后设置选定的值的 id。
this.f.descripcionForm.setValue(claseDeEstado.id);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.