Skip to main content

angular material - How to use Slider change event

app.component.ts

import { Component } from '@angular/core';
import { MatSlider } from '@angular/material/slider';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styles: [`
    mat-slider{
      width: 400px;
    }
  `]
})


export class AppComponent { 
  value:number = 0;
  oldValue:number = this.value;
  message:string = "";

  onSliderValueChange(slider:MatSlider){
    this.message = "Slider value change from "
      + this.oldValue + " to " + slider.value;
    this.oldValue = slider.value
  }
}
app.component.html

<h2 style="padding-top:25px; padding-bottom:25px;">
    Angular Material - How to use Slider change event
</h2>


<h3>Selected Value : {{value}}</h3>

<h3>{{message}}</h3>

<mat-slider
    #slider
    [(ngModel)]="value"
    (change)="onSliderValueChange(slider)">
</mat-slider>
app.module.ts [import]

import { MatSliderModule } from '@angular/material/slider';
import { FormsModule } from '@angular/forms';
More Angular tutorials