Angular Material Select - 默认值 - Reactive Forms [英] Angular Material Select - Default Value - Reactive Forms
问题描述
我在这里失去了它.关于这个主题的问题太多了,无论我怎么尝试都无法解决.
我只是想设置 mat-select 的默认值.
我有一份员工名单.单击列表项会打开一个对话框(员工详细信息),以便我可以编辑(或添加)员工的数据.我只是想让部门垫选择在模式为编辑时显示员工的当前部门.
employee-details.component.html
<div class="flex-item" fxFlex mat-dialog-actions fxLayoutAlign="end"><button mat-mini-fab color="" aria-label="Cancel" (click)="onCancelClick()"><mat-icon>clear</mat-icon>按钮><h4 *ngIf="data.action == 'add'">添加新员工</h4><h4 *ngIf="data.action == 'edit'">编辑员工</h4><form [formGroup]="form" class="employee-form"><div class="form-group"><mat-form-field class="employee-full-width"><输入表单控件名称=名字"垫输入必需的占位符=员工名字"><mat-error*ngIf="form.get('firstname').touched &&form.get('firstname').invalid">员工名字是必需的!</mat-error></mat-form-field><mat-form-field class="employee-full-width"><输入formControlName="姓氏"垫输入必需的占位符="员工姓氏"><mat-error*ngIf="form.get('lastname').touched &&form.get('lastname').invalid">员工姓氏为必填项!</mat-error></mat-form-field><mat-form-field class="employee-full-width"><输入表单控件名称=标题"垫输入必需的占位符=员工职称"><mat-error*ngIf="form.get('title').touched &&form.get('title').invalid">员工职称是必填项!</mat-error></mat-form-field><mat-form-field class="employee-full-width"><输入表单控件名称=比率"垫输入必需的占位符=员工率"><mat-error*ngIf="form.get('rate').touched &&form.get('rate').invalid">员工率是必需的!</mat-error></mat-form-field><mat-form-field class="employee-full-width"><mat-label>部门</mat-label><mat-select formControlName="部门"><mat-option *ngFor="let dept of Department" [value]="dept">{{ 部门?.title }}</mat-option></mat-select></mat-form-field>
</表单><div class="flex-item" fxFlex mat-dialog-actions fxLayoutAlign="end"><按钮垫高按钮颜色=次要"类=项目添加按钮"(点击)=onNoClick()">取消按钮><按钮垫高按钮[禁用]="!form.valid"颜色=主要"类=项目添加按钮"(点击)=创建员工(表单)">保存&关闭按钮><按钮垫高按钮[禁用]="!form.valid"颜色=主要"类=项目添加按钮"(点击)=创建员工(表单)">保存&新的按钮>
<div><div><br/><br/><垫手风琴><垫子扩展面板><mat-expansion-panel-header><mat-panel-title>表单值</mat-panel-title></mat-expansion-panel-header><代码><div fxLayout="column" fxLayoutGap="32px"><div>{{ form.value |json }}</div><div>表单有效性:{{ form.valid |json}}</div>