Angular Material: mat-select default value when using reactive forms
You need to define the criteria based on which an object becomes considered as selected using the [compareWith]
directive as follows:
<form [formGroup]="parentForm">
<mat-form-field class="example-full-width">
<input matInput formControlName="name" placeholder="Name">
</mat-form-field>
<mat-form-field class="example-full-width">
<mat-label>Users</mat-label>
<mat-select [compareWith]="compareFunction" formControlName="owners" multiple>
<mat-option *ngFor="let user of users" [value]="user"> {{ user.username }}</mat-option>
</mat-select>
</mat-form-field>
</form>
and define your compareFunction:
compareFunction(o1: any, o2: any) {
return (o1.name == o2.name && o1.id == o2.id);
}