简体   繁体   English

如何以 Angular 反应形式将默认选定值设置为 mat-select?

[英]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.

相关问题 我如何以反应形式访问模板中的异步数据以设置 mat-select 的选定值 - How can i access in reactive form async data in template to set selected value for mat-select 角度7:当value是具有多个ID而不是单个ID的对象时,如何在“反应形式”垫选中设置默认值? - Angular 7: How to set default value on Reactive Form mat-select when value is an object with several IDs rather than a single ID? 角材料垫选择设置默认选择值 - Angular Material mat-select set default selected value 如何通过在 mat-select 中选择的 ID 获取对象值并将对象值设置为反应形式? - How to take an object value by Its ID selected in mat-select and set the object values into a reactive form? angular mat-select 在表单中设置默认值 - angular mat-select set default value in form Angular Material Select (mat-select) - 如何设置默认值 - Angular Material Select (mat-select) - How to set default value 如何为 mat-select 设置默认值? - How to set default value for mat-select? Angular 11 mat-select 的选定默认值填充了 observable - Angular 11 selected default value of mat-select populated with observable Angular 6 材质<mat-select>使用表单控件多组默认值 - Angular 6 Material <mat-select> multiple set default value using form control Angular:如何在 mat-dialog 上的 mat-select 中返回所选项目的所有数据,同时保留所选默认值的代码 - Angular: How do I return all data for selected item in mat-select on mat-dialog whilst keeping code for selected default value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM