Forms Support

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

Template-Driven Forms

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

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

@Component({
    selector: 'my-app',
    template: `
        <form #form="ngForm">
            <div class="example-config">
                <strong>{{ value | json }}</strong>
            </div>
            <label>
              Select value:
                 <kendo-slider
                  name="masked"
                  [min]="0"
                  [max]="10"
                  [smallStep]="2"
                  [(ngModel)]="value"
                >
                </kendo-slider> <br />
              </label>
        </form>
    `
   })
export class AppComponent {
    public 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 Slider in a reactive form.

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

   const SliderValidator = minValid => control => {
       return control.value < minValid ? null : {
           valid: false
       }
   };

   @Component({
       selector: 'my-app',
       template: `
       <form [formGroup]="form">
           <div class="example-config">
               <p>Increase or decrease slider value to validate. Only values smaller than 5 are valid.</p>
               <p>The form is valid: <strong>{{form.controls.slider.valid}}</strong></p>
           </div>
           <kendo-slider formControlName="slider" [min]="0" [max]="10" [smallStep]="1"></kendo-slider>
       </form>
       `
   })
   class AppComponent {
       public form = new FormGroup({
           slider: new FormControl(5, SliderValidator(5))
       })
   }

FormField Association

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


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

@Component({
    selector: 'my-app',
    template: `
        <div class="example">
            <form class="k-form" [formGroup]="form">
                <kendo-formfield>
                    <kendo-label [for]="price" text="Price Limit"></kendo-label>
                    <kendo-slider [formControlName]="'price'" [min]="0" [max]="600" [smallStep]="50"></kendo-slider>
                    <kendo-formhint>Choose a price limit</kendo-formhint>
                </kendo-formfield>
            </form>
        </div>
    `,
    styles: [`
        .example {
            display: flex;
            justify-content: center;
        }
        .k-form { width: 400px; }
    `],
    encapsulation: ViewEncapsulation.None
})

export class AppComponent {
    public form: FormGroup;

    public userData: any = {
        price: null
    };

    constructor() {
        this.form = new FormGroup({
            price: new FormControl(this.userData.price)
        });
    }
}
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);

In this article