CheckBox Overview

The CheckBox enables you to style input type="checkbox" elements.

Basic Usage

The CheckBox lets the user toggle between checked and unchecked states. All regular input type="checkbox" HTML attributes and Angular bindings are applicable.

Styling Input Elements

The following example demonstrates the CheckBox directive in action.

@Component({
    selector: 'my-app',
    template: `
        <input type="checkbox" kendoCheckBox />
    `
})
class AppComponent {
}

Forms Support

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

Template Forms

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

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

@Component({
    selector: 'my-app',
    template: `
      <input type="checkbox" kendoCheckBox [(ngModel)]="model.foo" />
      <p>Model: {{model | json}}</p>
    `
})
class AppComponent {
    public model = {
        foo: 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 CheckBox in a reactive form.

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

@Component({
    selector: 'my-app',
    template: `
    <form [formGroup]="formGroup">
      <input type="checkbox" kendoCheckBox formControlName="foo" />
    </form>
    <p>Form value: {{formGroup.value | json}}</p>
    `
})
class AppComponent {
    public formGroup = new FormGroup({
        foo: new FormControl(true)
    });
}

Required CheckBox

You can make the CheckBox required.

To do so use one of the following:

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

@Component({
    selector: 'my-app',
    template: `
    <form [formGroup]="formGroup">
      <input type="checkbox" required formControlName="foo" kendoCheckBox />
      <input type="checkbox" [required]="isRequired" formControlName="bar" kendoCheckBox />
      <input type="checkbox" formControlName="baz" kendoCheckBox />
    </form>
    <p>Form value: {{formGroup.value | json}}; Valid: {{formGroup.valid}}</p>
    `,
    styles: [`
        form {
            width: 100px;
            padding: 5px;
            text-align: center;
        }
        form.ng-invalid {
            border: 1px solid red;
        }
    `]
})
class AppComponent {
    public isRequired = true;
    public formGroup = new FormGroup({
        foo: new FormControl(),
        bar: new FormControl(),
        baz: new FormControl(null, Validators.required)
    });
}

Associating with Labels

The CheckBox can be associated with a HTML Label element like the regular <input type="checkbox">.

@Component({
    selector: 'my-app',
    template: `
      <label>CheckBox wrapped within a Label: 
        <input type="checkbox" kendoCheckBox />
      </label>
      <br /><br />
      <label for="foo">Checkbox and Label with matching "id" and "for" attributes </label>
        <input id="foo" type="checkbox" kendoCheckBox />
    `
})
class AppComponent {
}

Indeterminate State

The CheckBox can display an indeterminate state when the respective property is set to true.

The indeterminate state is independent of the checked state and upon user interaction the current checked state will be displayed.

@Component({
    selector: 'my-app',
    template: `
        <input id="foo" type="checkbox" [indeterminate]="true" [(ngModel)]="checked" kendoCheckBox />
        <p>Checked: {{checked}}</p>
    `
})
class AppComponent {
    public checked = false;
}

Disabled CheckBox

By default, the CheckBox is enabled.

To disable user interaction with the element, use the disabled attribute or Angular property binding.

@Component({
    selector: 'my-app',
    template: `
        <input type="checkbox" disabled kendoCheckBox />
        <input type="checkbox" [disabled]="isDisabled" kendoCheckBox />
        <p>The CheckBoxes are disabled</p>
    `
})
class AppComponent {
    public isDisabled = true;
}

In this article