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.

Associating with Labels

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

import { Component, ViewEncapsulation } from '@angular/core';

@Component({
    selector: 'my-app',
    template: `
        <div class="section">
            <h5>Association with native HTML label</h5>
            <div class="wrap">
                <label class="k-checkbox-label" for="terms">I accept Terms&Conditions</label>
                <input type="checkbox" id="terms" kendoCheckBox />
            </div>
        </div>

        <div class="section">
            <h5>Association with the Label component</h5>
            <div class="wrap">
                <kendo-label class="k-checkbox-label" [for]="terms" text="I accept Terms&Conditions"></kendo-label>
                <input type="checkbox" #terms kendoCheckBox />
            </div>
        </div>
    `,
    encapsulation: ViewEncapsulation.None,
    styles: [`
        .section {
            margin-top: 20px;
        }

    `]
})
export class AppComponent {}
import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

import { LabelModule } from '@progress/kendo-angular-label';
import { InputsModule } from '@progress/kendo-angular-inputs';
import { AppComponent } from './app.component';

@NgModule({
    bootstrap:    [AppComponent],
    declarations: [AppComponent],
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        CommonModule,
        FormsModule,
        ReactiveFormsModule,
        InputsModule,
        LabelModule
    ]
})
export class AppModule {}
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';

enableProdMode();

const platform = platformBrowserDynamic();
platform.bootstrapModule(AppModule);

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: `
        <label class="k-label">Accept Terms&Conditions
            <input type="checkbox" kendoCheckBox [(ngModel)]="model.terms" />
        </label>
        <p>Model: {{model | json}}</p>
    `
})
class AppComponent {
    public model = {
        terms: 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">
        <label class="k-label">
            Allow Notifications
            <input type="checkbox" kendoCheckBox formControlName="notifications" />
        </label>
    </form>
    <p>Form value: {{formGroup.value | json}}</p>
    `
})
class AppComponent {
    public formGroup = new FormGroup({
        notifications: new FormControl(true)
    });
}

FormField Association

The following example demonstrates how to use the CheckBox within a FormField component and provide hint and error messages.

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

@Component({
    selector: 'my-app',
    template: `
        <div class="example">
            <form class="k-form" [formGroup]="form">
                <kendo-formfield [showHints]="'initial'">
                    <div class="k-form-field-checkbox-wrap">
                        <input kendoCheckBox id="terms" type="checkbox" [formControlName]="'terms'" />
                        <kendo-label class="k-checkbox-label" for="terms" text="I agree with Terms&Conditions"></kendo-label>
                    </div>

                    <kendo-formhint>By checking this, you agree to our Terms&Conditions</kendo-formhint>
                    <kendo-formerror>Error: This field is required</kendo-formerror>
                </kendo-formfield>
            </form>
        </div>
    `,
    styles: [`
        .k-form {
            width: 350px;
            padding: 20px;
        }
    `]
})
export class AppComponent {
    public form: FormGroup;

    public data: any = {
        terms: '',
    };

    constructor() {
        this.form = new FormGroup({
            terms: new FormControl(this.data.terms, [Validators.requiredTrue]),
        });
    }
}
import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

import { LabelModule } from '@progress/kendo-angular-label';
import { InputsModule } from '@progress/kendo-angular-inputs';
import { AppComponent } from './app.component';

@NgModule({
    bootstrap:    [AppComponent],
    declarations: [AppComponent],
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        CommonModule,
        FormsModule,
        ReactiveFormsModule,
        InputsModule,
        LabelModule
    ]
})
export class AppModule {}
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';

enableProdMode();

const platform = platformBrowserDynamic();
platform.bootstrapModule(AppModule);

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">
        <label class="k-label">
            I agree to Terms&Conditions
            <input type="checkbox" formControlName="terms" kendoCheckBox />
        </label>
    </form>
    <p>Form value: {{formGroup.value | json}}; Valid: {{formGroup.valid}}</p>
    `,
    styles: [`
        form {
            width: 220px;
            padding: 5px;
            text-align: center;
        }
        form.ng-invalid {
            border: 1px solid red;
        }
    `]
})
class AppComponent {
    public formGroup = new FormGroup({
        terms: new FormControl(null, Validators.required)
    });
}

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 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