Forms Support

You can use the DropDownList 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 falsejust like the defaultItem, the value has to match the data type.

Template-Driven Forms

The template-driven forms enable you to bind the DropDownList 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.

Example
View Source
Edit In Stackblitz  
Change Theme:

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: `
    <div class="example-config">
        The selected gender is <strong>{{ gender | json }}</strong>
    </div>
    <form #form="ngForm">
        <kendo-label [for]="genderList" [text]="'Select Gender'"></kendo-label>
        <kendo-dropdownlist
            #genderList
            name="gender"
            [data]="genders"
            [textField]="'text'"
            [valueField]="'value'"
            [valuePrimitive]="true"
            [(ngModel)]="gender"
        >
        </kendo-dropdownlist>
    </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 DropDownList in a reactive form with a primitive value binding.

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

@Component({
  selector: 'my-app',
  template: `
    <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>
    <form [formGroup]="myForm">
        <kendo-label [for]="gender" [text]="'Select Gender'"></kendo-label>
        <kendo-dropdownlist
            #gender
            formControlName="gender"
            [data]="genders"
            [defaultItem]="{ text: 'Select gender', value: null }"
            [textField]="'text'"
            [valueField]="'value'"
            [valuePrimitive]="true"
            required
        >
        </kendo-dropdownlist>
    </form>
  `
})
class AppComponent {
    public genders: Array<{ text: string, value: number }> = [
        { text: 'Male', value: 1 },
        { text: 'Female', value: 2 }
    ];

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

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

The following example demonstrates how to use the DropDownList in a reactive form with a complex value binding and required validation.

The required built-in validator does not support object validation. If a defaultItem is set, the developer needs to implement the required custom validation, as demonstrated in the example.

FormField Association

The following example demonstrates the usage of the DropDownList within a FormField with hint and error messages.

In this article

Not finding the help you need?