All Components

Forms Support

You can use the ComboBox in template-driven or reactive forms.

The component accepts values of the complex (objects) or the primitive (strings or numbers) type. To specify the value type, set the valuePrimitive property. By default, it is set to false—the value has to match the data type.

Template-Driven Forms

The template-driven forms enable you to bind the ComboBox to the model by using the ngModel directive.

The following example demonstrates how to accomplish a two-way data binding when the model field is a complex value.

@Component({
  selector: 'my-app',
  template: `
    <form #form="ngForm">
        <div class="example-config">
            The selected gender is <strong>{{ gender | json }}</strong>
        </div>
        <div class="example-wrapper">
            <label>
                Select Gender:
                <kendo-combobox
                    name="gender"
                    [data]="genders"
                    [textField]="'text'"
                    [valueField]="'value'"
                    [(ngModel)]="gender"
                >
                </kendo-combobox>
            </label>
        </div>
    </form>
  `
})
class AppComponent {
    public genders: Array<{ text: string, value: number }> = [
        { text: "Male", value: 1 },
        { text: "Female", value: 2 },
        { text: "Other", value: 3 }
    ];

    public gender: { text: string, value: number } = { text: "Female", value: 2 };
}

The following example demonstrates how to accomplish a two-way data binding when the model field is a primitive value.

@Component({
  selector: 'my-app',
  template: `
    <form #form="ngForm">
        <div class="example-config">
            The selected gender is <strong>{{ gender | json }}</strong>
        </div>
        <label>
            Select Gender:
            <kendo-combobox
                name="gender"
                [data]="genders"
                [textField]="'text'"
                [valueField]="'value'"
                [valuePrimitive]="true"
                [(ngModel)]="gender"
            >
            </kendo-combobox>
        </label>
    </form>
  `
})
class AppComponent {
    public genders: Array<{ text: string, value: number }> = [
        { text: "Male", value: 1 },
        { text: "Female", value: 2 },
        { text: "Other", value: 3 }
    ];

    public gender: { text: string, value: number } = 2;
}

Reactive Forms

The FormGroup provides a way to render reactive forms. For more details, refer to the Angular Documentation.

The following example demonstrates how to use the ComboBox in a reactive form with a primitive value binding.

import {
    FormsModule,
    ReactiveFormsModule,
    FormGroup,
    FormControl
} from '@angular/forms';

@Component({
  selector: 'my-app',
  template: `
    <form [formGroup]="myForm">
        <div class="example-config">
            <p>The form is valid: <strong>{{myForm.controls.gender.valid}}</strong></p>
            <p>The form.gender value is: <strong>{{myForm.controls.gender.value}}</strong></p>
        </div>
        <label>
            Select gender
            <kendo-combobox
                formControlName="gender"
                [data]="genders"
                [textField]="'text'"
                [valueField]="'value'"
                [valuePrimitive]="true"
                required
            >
            </kendo-combobox>
        </label>
    </form>
  `
})
class AppComponent {
    public genders: Array<{ text: string, value: number }> = [
        { text: "Male", value: 1 },
        { text: "Female", value: 2 },
        { text: "Other", value: 3 }
    ];

    public myForm: FormGroup = new FormGroup({
        gender: new FormControl(2)
    });
}

The following example demonstrates how to use the ComboBox in a reactive form with a complex value binding.

import {
    FormsModule,
    ReactiveFormsModule,
    FormGroup,
    FormControl
} from '@angular/forms';

@Component({
  selector: 'my-app',
  template: `
    <form [formGroup]="myForm">
        <div class="example-config">
            <p>The form is valid: <strong>{{myForm.controls.gender.valid}}</strong></p>
            <p>The form.gender value is: <strong>{{myForm.controls.gender.value | json}}</strong></p>
        </div>
        <label>
            Select gender
            <kendo-combobox
                formControlName="gender"
                [data]="genders"
                [textField]="'text'"
                [valueField]="'value'"
                required
            >
            </kendo-combobox>
        </label>
    </form>
  `
})
class AppComponent {
    public genders: Array<{ text: string, value: number }> = [
        { text: "Male", value: 1 },
        { text: "Female", value: 2 },
        { text: "Other", value: 3 }
    ];

    public myForm: FormGroup = new FormGroup({
        gender: new FormControl({ text: "Female", value: 2 })
    });
}
In this article