我有一个选择标签,想在ngOnInit()
中为其设置值。
我的选择如下:
<div class="form__field">
<label class="form__label">Project</label>
<mat-form-field>
<mat-select formControlName="project">
<mat-option *ngFor="let project of projects" [value]="project">
{{project.name}}
</mat-option>
</mat-select>
</mat-form-field>
</div>
项目所在的对象具有
之类的字段{
author: 'name',
name: 'name',
participants: {'', ''},
uid: '',
projectId:''
}
我在.ts文件中设置了一个值,但是它不起作用。 (它适用于其他字段,但不适用于此选择)我尝试过类似this.task.projectFull.name...
this.form.setValue({
name: this.task.name,
priority: this.task.priority,
assignee: this.task.assignee,
project: this.task.projectFull, //projectFull: this.form.value.project
});
this.task
是我从Firestore获得的任务,其界面如下:
export default interface Task {
id?: string;
name?: string;
createdBy?: string;
completed?: boolean;
priority?: string;
assignee?: string;
projectFull?;
project?: string;
projectId?: string;
}
非常感谢您的帮助!
答案 0 :(得分:0)
您需要提供compareWith
函数,如下所述:Customizing option selection
<mat-select [compareWith]="compareFn" formControlName="project">
<mat-option *ngFor="let project of projects" [value]="project">
{{project.name}}
</mat-option>
</mat-select>
compareFn(p1: Project, p2: Project): boolean {
return p1 && p2 ? p1.projectId === p2.projectId : p1 === p2;
}