I have a problem with Angular 6: if I use a component based on the <select>
element (combo-box), if I use it with the classic way, everything works (notice that I used the selected
attribute: the result is that in the template the default option already appears as selected, as I would expect!):
<select data-width="200px" title="title" name = "name"
>
<option value='default' selected>Default</option>
<option value='1'>Value 1</option>
<option value='2'>Value 2</option>
</select>
The problem is that if I use the [(ngModel)]
directive (
it needs me necessarily to get the value entered in the combo box), for some reason the selected attribute does not seem to work anymore and the combo box appears with an empty value as the selected
option.
<select data-width="200px" title="title" name = "name"
[(ngModel)] = "value"
>
<option value='default' selected>Default</option>
<option value='1'>Value 1</option>
<option value='2'>Value 2</option>
</select>
This is certainly not the behavior I would like to have a default option selected preserving the ngModel
directive.
How can I solve this problem?