Forms Support

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

Template-Driven Forms

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

The following example demonstrates how to use the Switch in a template-driven form.

@Component({
    selector: 'my-app',
    template: `
        <form #form="ngForm">
            <div class="example-config">
                <p>The switch value is: <strong>{{ value }}</strong></p>
            </div>
            <label>
              Select value:
                 <kendo-switch
                  name="switch"
                  [(ngModel)]="value"
                >
                </kendo-switch> <br />
              </label>
        </form>
    `
   })
export class AppComponent {
    public value: number = true;
}

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 Switch in a reactive form.

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

   @Component({
       selector: 'my-app',
       template: `
       <form [formGroup]="form">
           <div class="example-config">
               <p>The form is valid: <strong>{{ form.controls.switch.valid }}</strong></p>
               <p>The form switch value is: <strong>{{ form.controls.switch.value }}</strong></p>
           </div>
           <kendo-switch formControlName="switch"></kendo-switch>
       </form>
       `
   })
   class AppComponent {
       public form = new FormGroup({
           switch: new FormControl(null, Validators.required)
       })
   }

In this article