Angular select option with selected attribute not working
If you don't wish to have a two-way binding, you can set ngModel to the 'default' value and with the template local variable get the selected value:
<select #rate ngModel="hr">
<option selected value="hr">hr</option>
<option value="yr">yr</option>
</select>
<p *ngIf="rate.value == 'hr'">hr</p>
<p *ngIf="rate.value == 'yr'">yr</p>
{{rate.value}}
DEMO
When you use ngModel
, the state is handled internally and any explicit change applied to the attributes just gets ignored. In your example, you are setting the selected property of option, but you are also providing a (void) ngModel to your select, so Angular expects that the state of the select is provided within the ngModel.
Briefly, you should leverage on your ngModel rather than setting the selected property:
<select
name="rate"
#rate="ngModel"
[(ngModel)]="yourModelName"
required
>
<option value="hr">hr</option>
<option value="yr">yr</option>
</select>
And:
yourModelName: string;
constructor() {
this.yourModelName = 'hr';
}