In my angular 6 project I have a mat-select and I have to use the compareWith function because I am selecting objects.
But at the moment if I don't select anything it seems that the first option is selected, I don't set manually any value in the field and I see the compare function is comparing the first option...and I don't want this behavior.
The form correctly still has null value in this field.
Is this an expected behavior or I am missing something?
P.S. I can't add an option like null to my options.
html
<mat-form-field>
<mat-select (selectionChange)="fareExceptionHandler(); setFareExceptionCode($event)" formControlName="fareException" [compareWith]="displayFn"
placeholder="{{'ticket.new.labels.fareexception' | translate }}" name="fare">
<mat-option *ngFor="let fare of fareExceptionsList" [value]="fare">
{{ fare.name }} </mat-option>
</mat-select>
</mat-form-field>
compare function
displayFn(model: any) {
if ((model !== undefined) && (model !== null)) {
return model.name;
}
}
***********************************************EDIT*******************************************
this is a similar example https://stackblitz.com/edit/angular-rykism?file=app%2Fselect-overview-example.ts