In my case I'm using the same markup for editing an existing entity, or creating a brand new one (so far anyway).
I want to display a default-value that is "selected" in the select-elements when in creationMode, while displaying the values form the backend in editMode.
My solution is:
<select [(ngModel)]="entity.property" id="property" name="property" required>
<option disabled hidden value="undefined">Enter prop</option>
<option *ngFor="let prop of sortedProps" value="{{prop.value}}">{{prop.displayName}}</option>
</select>
for the regular available options I'm using a sortedProps
Array to provide option choices. But that's not the important part here.
What did the trick for me is setting value="undefined"
to let the angular-model-binding (?) select this option automatically when in creationMode. Not sure if its a hack, but does exactly what i want. No addtionally typeScript necessary.
Additionally, sepcifing hidden
makes sure the option in my case is not selectable, while required
makes sure the form is invalid unless something gets selected there.
nrSelect
to the default value. By the way, if you want binding to work, you ned to change it to[(ngModel)]
as well – Kenny