Value Binding
The value of the ComboBox can either be a primitive (strings, numbers, or other) or a complex value (objects).
To set the value, apply any of the following approaches:
- Use the
value
property. If the value is set through thevalue
property, you have to hook up to thevalueChange
event and manually update the value of thevalue
property. - Use the
ngModel
value binding. If the value is set by thengModel
value binding, the framework will automatically update the corresponding field from the model after the value of the component changes. - Use the
formControlName
value binding, which is available in the Reactive forms. If the value is set by theformControlName
value binding, the framework will automatically update the corresponding field from the form model after the value of the component changes.
- The ComboBox does not support the simultaneous usage of the
value
property and thengModel
value binding.- The ComboBox does not support values, which contain
\n
new line characters. The regularinput
HTML DOM element, which the ComboBox uses internally treats\n
as a regular whitespace. Therefore, when the component compares the value of theinput
element with the data item that is selected by the user, the two values differ. The value of theinput
element is actually not present in the data of the component and the ComboBox does not recognize it. To handle such issues, map the incoming data so that it contains regular whitespaces only.
When binding the ComboBox, the component provides options for:
- Using primitive values (string, numbers, or other)
- Using complex values (objects)
- Using primitive values from object fields
- Using custom values
- Handling invalid value errors
- Handling unresolved text
Primitive Values
If the ComboBox is bound to a dataset of primitives, its value will be a primitive of the same type.
Primitive data types include:
String
Number
Undefined
Null
Object Values
If the ComboBox is bound to a dataset of objects, its value will be an object of the same type.
When the selected item is an object, always specify
valueField
. If you do not set a value for the field, the ComboBox will compare the items by reference, which may complicate debugging. For example, the selected value will not be applied, if it does not reference the exact passeddata
object.
Primitive Values from Object Fields
If you bind the ComboBox to a dataset of objects and the valuePrimitive
property is set to true
, the value of the component will be extracted from the valueField
of the objects.
Invalid Value Errors
If the value, which is assigned through the [value]
or [(ngModel)]
inputs, does not match the expected type, the ComboBox throws a JavaScript error.
In the following example, the component has both its [valueField]
and [textField]
specified, which implies that the [data]
will contain objects. Because the [valuePrimitive]
is not explicitly set to true
, the ComboBox expects an object value. Instead, a number is provided and, as a result, a JavaScript exception occurs.
To fix the JavaScript issue, either:
- Change the value type, or
- Update the settings of the component.
The following table lists the valid configuration scenarios.
Value | [Data] | [ValuePrimitive] |
---|---|---|
primitive | primitives | Not set (automatically calculated as true ) |
object | objects | Not set (automatically calculated as false ) |
primitive | objects | true (manually set by the developer) |
Unresolved Text
The text value, which is assigned through the [value]
or [(ngModel)]
inputs and which is not found in the dataset will not be resolved. Such values also include object values that contain the textField
property.
The following example demonstrates how to merge the assigned value with the dataset and ensure that its text will be properly extracted and displayed.