My template is like this
<select>
<option>AM</option>
<option>PM</option>
</select>
How can I set option AM selected by default?
<select [(ngModel)]="defaultValue">
<option>AM</option>
<option>PM</option>
</select>
export class MyComponent {
defaultValue = 'PM';
}
Usually it's done like
<select [(ngModel)]="defaultValue">
<option *ngFor="let value of values" [ngValue]="value">{{value}}</option>
</select>
export class MyComponent {
values = ['AM', 'PM'];
defaultValue = this.values[1];
}
Hint For non-string values use `[ngValue]="..."
<select [(ngModel)]="defaultValue">
<option [ngValue]="values[0]">AM</option>
<option [ngValue]="values[2]">PM</option>
</select>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With