![](/img/trans.png)
[英]Angular 4 - Select default value in dropdown [Reactive Forms]
[英]Angular Material Select - Default Value - Reactive Forms
我在這里失去它。 關於這個主題有很多問題,無論我嘗試什么,我都無法讓它發揮作用。
我只是想設置 mat-select 的默認值。
我有一份員工名單。 單擊列表項會打開一個對話框(員工詳細信息),以便我可以編輯(或添加)員工的數據。 我只希望部門墊選擇在模式為編輯時顯示員工的當前部門。
員工詳細信息.component.html
<div fxLayout="column">
<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>
</button>
</div>
<h4 *ngIf="data.action == 'add'">Add New Employee</h4>
<h4 *ngIf="data.action == 'edit'">Edit Employee</h4>
<form [formGroup]="form" class="employee-form">
<div class="form-group">
<mat-form-field class="employee-full-width">
<input
formControlName="firstname"
matInput
required
placeholder="Employee Firstname"
>
<mat-error
*ngIf="
form.get('firstname').touched && form.get('firstname').invalid
"
>Employee Firstname is Required!</mat-error
>
</mat-form-field>
<mat-form-field class="employee-full-width">
<input
formControlName="lastname"
matInput
required
placeholder="Employee Lastname"
>
<mat-error
*ngIf="
form.get('lastname').touched && form.get('lastname').invalid
"
>Employee Lastname is Required!</mat-error
>
</mat-form-field>
<mat-form-field class="employee-full-width">
<input
formControlName="title"
matInput
required
placeholder="Employee Title"
>
<mat-error
*ngIf="
form.get('title').touched && form.get('title').invalid
"
>Employee Title is Required!</mat-error
>
</mat-form-field>
<mat-form-field class="employee-full-width">
<input
formControlName="rate"
matInput
required
placeholder="Employee Rate"
>
<mat-error
*ngIf="
form.get('rate').touched && form.get('rate').invalid
"
>Employee Rate is Required!</mat-error
>
</mat-form-field>
<mat-form-field class="employee-full-width">
<mat-label>Department</mat-label>
<mat-select formControlName="department">
<mat-option *ngFor="let dept of departments" [value]="dept">
{{ dept?.title }}
</mat-option>
</mat-select>
</mat-form-field>
</div>
</form>
<div class="flex-item" fxFlex mat-dialog-actions fxLayoutAlign="end">
<button
mat-raised-button
color="secondary"
class="project-add-button"
(click)="onNoClick()"
>
Cancel
</button>
<button
mat-raised-button
[disabled]="!form.valid"
color="primary"
class="project-add-button"
(click)="createEmployee(form)"
>
Save & Close
</button>
<button
mat-raised-button
[disabled]="!form.valid"
color="primary"
class="project-add-button"
(click)="createEmployee(form)"
>
Save & New
</button>
</div>
</div>
<div>
<div>
<br />
<br />
<mat-accordion>
<mat-expansion-panel>
<mat-expansion-panel-header>
<mat-panel-title>
Form value
</mat-panel-title>
</mat-expansion-panel-header>
<code>
<div fxLayout="column" fxLayoutGap="32px">
<div>{{ form.value | json }}</div>
<div>Form Validity: {{ form.valid | json}}</div>
</div>
</code>
</mat-expansion-panel>
</mat-accordion>
</div>
</div>
員工詳細信息.component.ts
import { Component, OnInit, Optional, Inject, AfterViewInit } from '@angular/core';
import { FormGroup, FormControl, Validators } from '@angular/forms';
import { EmployeeService } from 'src/app/core/services/entity/employee.service';
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material';
import { DialogData } from 'src/app/employees/employee-list/employee-list.component';
import { DepartmentService } from 'src/app/core/services/entity/department.service';
import { Department } from 'src/app/shared/models/department';
@Component({
selector: 'app-employee-details',
templateUrl: './employee-details.component.html',
styleUrls: ['./employee-details.component.css']
})
export class EmployeeDetailsComponent implements OnInit, AfterViewInit {
departments: Department[];
selectedDepartment: Department;
form = new FormGroup({
id: new FormControl(''),
firstname: new FormControl(''),
lastname: new FormControl(''),
title: new FormControl(''),
rate: new FormControl(''),
department: new FormControl(''),
});
constructor(
private departmentService: DepartmentService,
private service: EmployeeService,
public dialogRef: MatDialogRef<EmployeeDetailsComponent>,
@Optional() @Inject(MAT_DIALOG_DATA) public data: DialogData
) {
if (data.action === 'edit') {
this.form.patchValue(data.employee);
}
}
ngOnInit() {
this.departmentService.departments$.subscribe(r => {
this.departments = r;
});
}
// **** THIS DOESN'T ACTUALLY DO ANYTHING, AS FAR AS I CAN TELL ****
ngAfterViewInit() {
this.form.controls.department.setValue(this.data.employee.department);
}
createEmployee(f: FormGroup) {
if (this.data.action === 'edit') {
this.service.updateEmployee(f.value).then(response => {
this.closeDialog();
});
// creating a new one
} else {
this.service.createEmployee(f.value).then(response => {
this.closeDialog();
});
}
}
onCancelClick() {
this.onNoClick();
}
onNoClick() {
this.dialogRef.close({
event: 'cancel'
});
}
closeDialog() {
this.dialogRef.close({
event: this.data.action,
data: this.data.employee
});
}
}
我正在使用以下命令在對話框底部顯示“數據”:
<div>{{ form.value | json }}</div>
它看起來像(它告訴我 Employee 對象正在將其轉移到對話框中):
{
"id": "9UAPoKCFofH7aWBUwdiO",
"firstname": "John",
"lastname": "Doe",
"title": "Manager",
"rate": "120",
"department": {
"code": "1000",
"id": "2PtDUwPBXT7pUPWTOibD",
"title": "Project Management"
}
}
為了填充部門 mat-select,我使用了一個 CRUDs Firebase 服務:
ngOnInit() {
this.departmentService.departments$.subscribe(r => {
this.departments = r;
});
}
模板中的相關 HTML:
<mat-form-field class="employee-full-width">
<mat-label>Department</mat-label>
<mat-select formControlName="department">
<mat-option *ngFor="let dept of departments" [value]="dept">
{{ dept?.title }}
</mat-option>
</mat-select>
</mat-form-field>
員工.ts
import { Department } from './department';
export interface Employee {
id: string;
firstname: string;
lastname: string;
title: string;
department: Department;
rate: number;
}
部門.ts
export interface Department {
id: string;
title: string;
code: string;
}
原因是您的員工記錄中的部門對象與查詢結果中的部門對象不匹配。 是的,值匹配,但對象引用不匹配(它是一個單獨的對象)。
您可以使用 [compareWith] 屬性傳入一個比較對象 ID 的函數。 mat-select 支持與標准 Angular select 相同的 [compareWith] 功能。 https://angular.io/api/forms/SelectControlValueAccessor#customizing-option-selection
所以你會在你的模板中有這個:
<mat-select formControlName="department" [compareWith]="compareFn">
然后在您的組件中:
compareFn(d1: Department, d2: Department): boolean {
return d1 && d2 ? d1.id === d2.id : d1 === d2;
}
嘗試
ngAfterViewInit() {
this.form.controls['department'].setValue(this.data.employee.department);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.