Goal
I want to pre-select an option of a select with [ngValue] and [(ngModel)] with the values being an object TypeConge:
{ "id": 2, "libelle": "Holidays" }
First Try
<div class="form-group col-12">
<label class="font-weight-bold" for="typeConge"> Type congé </label>
<select class="form-control" id="typeConge" name="typeConge" [(ngModel)]="conge.typeConge" >
<option [ngValue]="null"></option>
<option [ngValue]="typeCongeOption" *ngFor="let typeCongeOption of typeConges; trackBy: trackTypeCongeById">{{typeCongeOption.libelle}}</option>
</select>
</div>
{{conge.typeConge| json}}
Problem: The select option is not preselected.
Second Try
<div class="form-group col-12">
<label class="font-weight-bold" for="typeConge"> Type congé </label>
<select class="form-control" id="typeConge" name="typeConge" [compareWith] = 'customCompareTypeConge' [(ngModel)]="conge.typeConge" >
<option [ngValue]="null"></option>
<option [ngValue]="typeCongeOption" *ngFor="let typeCongeOption of typeConges; trackBy: trackTypeCongeById">{{typeCongeOption.libelle}}</option>
</select>
</div>
{{conge.typeConge| json}}
...
customCompareTypeConge(tc1: TypeConge, tc2: TypeConge){
return tc1.id == tc2.id;
}
Problem: In the console I find: Cannot read property 'id' of null
EDIT
EDIT 2
A full explanation of this issue can be found at this adress.
Could you help me please?
Thanks!
this.conge.typeConge = this.typeConges[0]? Orthis.conge.typeConge = nullif you want to select the first option. - ConnorsFan