Angular ngModel not matching any selectbox options

138 Views Asked by At

I have a problem with selectboxes and an one-way-databinding via [ngModel] in angular:

If my model is storing a value that is not longer existing as an option, the selectbox still takes the value from the model and just shows an "empty" selected option and is valid (See this Plunker example).

Is this a bug? Am I doing something wrong? I would expect a different behavior, maybe not taking the value from the model at all or atleast marking the field as invalid. A selectbox shouldn't be valid if the value has no matching options in my opinion.

Is there an easy way to achieve this? I am thinking about adding a custom Validator (that is checking if the value is in a given list or something like that), but i want to avoid this if possible.

1

There are 1 best solutions below

0
On

Since Angular do not expose list of option values in form control better create options dynamically using some value map or list (something like dropValues=[1,2,3]) and match the value you trying to set against the map. Its kind of you have to write your own custom validation.