How To Use Ngx-slider With A Different Type Of Style In Angular

In this article, we are going to see how to use ngx-slider in angular and what options are available in ngx-slider. Please follow the below steps.
 
You will find an overview of using the slider directive, and following the navigation links, you can explore the public API portion of the library.
 
The public API is made up of all declarations made available when importing the ngx-slider package, for instance, the Options class.
 
First, we need to install the following npm reference for ngx-slider:
 
npm install --save @angular-slider/ngx-slider
 
 
Once you've installed the package please copy the below code to the app.module.ts file.
  1. import { NgModule } from '@angular/core';  
  2. import { BrowserModule } from '@angular/platform-browser';  
  3. import { AppRoutingModule } from './app-routing.module';  
  4. import { AppComponent } from './app.component';  
  5. import { NgxSliderModule } from "@angular-slider/ngx-slider";  
  6. @NgModule({  
  7.     declarations: [  
  8.         AppComponent  
  9.     ],  
  10.     imports: [  
  11.         BrowserModule,  
  12.         AppRoutingModule,  
  13.         NgxSliderModule  
  14.     ],  
  15.     providers: [],  
  16.     bootstrap: [AppComponent]  
  17. })  
  18. export class AppModule {}  
Please copy the app.component.ts & app.component.html file codes below.
  1. import { Component} from '@angular/core';  
  2. import { Options} from '@angular-slider/ngx-slider';  
  3. @Component({  
  4.     selector: 'app-root',  
  5.     templateUrl: './app.component.html',  
  6.     styleUrls: ['./app.component.css']  
  7. })  
  8. export class AppComponent {  
  9.     title = 'ngx-slider';  
  10.     value: number = 123;  
  11.     options: Options = {  
  12.         floor: 0,  
  13.         ceil: 250  
  14.     };  
  15. }  
app.component.html
  1. <ngx-slider [(value)]="value" [options]="options"></ngx-slider>  

Single slider

 
Here the options; we are going to set range 0 to 250 and set the range value is 123. this one of the single sliders. Once you add the changes please run the application using ng-serve --open the http://localhost:4200 will be open and changes were reflected in the browser.
 
 
The slider is available in more types of options; let's see others.
 

Range slider

 
The range slider set minimum and maximum between the range value in the ngx-slider. Here I have set the minimum value as 50 and maxvalue is 70 and the range is 0 to 100.
  1. value: number = 50;  
  2. maxvalue: number = 70;  
  3. options: Options = {  
  4.     floor: 0,  
  5.     ceil: 100  
  6. };  
  7. <ngx-slider [(value)]="value" [(highValue)]="maxvalue" [options]="options"></ngx-slider>  
 

Slider with ticks

 
The slider with ticks set different types of ticks like small, medium, average, large, and extra-large and the slider position is set number 5.
  1. value: number = 5;  
  2. options: Options = {  
  3.     showTicksValues: true,  
  4.     stepsArray: [{  
  5.         value: 1,  
  6.         legend: "Small"  
  7.     }, {  
  8.         value: 2  
  9.     }, {  
  10.         value: 3,  
  11.         legend: "Medium"  
  12.     }, {  
  13.         value: 4  
  14.     }, {  
  15.         value: 5,  
  16.         legend: "Average"  
  17.     }, {  
  18.         value: 6  
  19.     }, {  
  20.         value: 7,  
  21.         legend: "Large"  
  22.     }, {  
  23.         value: 8  
  24.     }, {  
  25.         value: 9,  
  26.         legend: "Extra Large"  
  27.     }]  
  28. };   
  29. <ngx - slider[(value)] = "value" [options] = "options" ></ngx-slider>  
 

Customized slider

 
The customized slider is usually used within a big presentation, a custom slideshow is a sequence of slides that expresses some exact idea or represents a particular subtopic.
 
Please check the below codes for setting options.
  1. minValue: number = 200;  
  2. maxValue: number = 300;  
  3. options: Options = {  
  4.     floor: 0,  
  5.     ceil: 500,  
  6.     translate: (value: number, label: LabelType): string => {  
  7.         switch (label) {  
  8.             case LabelType.Low:  
  9.                 return "<b>Min price:</b> $" + value;  
  10.             case LabelType.High:  
  11.                 return "<b>Max price:</b> $" + value;  
  12.             default:  
  13.                 return "$" + value;  
  14.         }  
  15.     }  
  16. };   
  17. <ngx-slider [(value)]="minValue" [(highValue)]="maxValue" [options]="options"></ngx-slider>  
 

Slider with custom style

 
In the custom style we able to change the slider style manually. Here I have added some of the styles app.components.scss. showTicks set true. The range between was a show with ticks. Please check the changes in the below image.
  1. value: number = 10;  
  2. highValue: number = 90;  
  3. options: Options = {  
  4.     floor: 0,  
  5.     ceil: 100,  
  6.     step: 10,  
  7.     showTicks: true  
  8. };   
  9. <div class="custom-slider">  
  10.    <ngx-slider [(value)]="value" [(highValue)]="highValue" [options]="options"></ngx-slider>  
  11. </div>  
app.component.scss
  1. ::ng - deep {  
  2.     .custom - slider.ngx - slider.ngx - slider - bar {  
  3.         background: #ffe4d1;  
  4.         height: 2 px;  
  5.     }.custom - slider.ngx - slider.ngx - slider - selection {  
  6.         background: orange;  
  7.     }.custom - slider.ngx - slider.ngx - slider - pointer {  
  8.         width: 8 px;  
  9.         height: 16 px;  
  10.         top: auto; /* to remove the default positioning */  
  11.         bottom: 0;  
  12.         background - color: #333;  
  13.         border-top-left-radius: 3px;  
  14.         border-top-right-radius: 3px;  
  15.       }  
  16.    .custom-slider .ngx-slider .ngx-slider-pointer:after {  
  17.       display: none;  
  18.    }  
  19.    .custom-slider .ngx-slider .ngx-slider-bubble {  
  20.       bottom: 14px;  
  21.    }  
  22.    .custom-slider .ngx-slider .ngx-slider-limit {  
  23.       font-weight: bold;  
  24.       color: orange;  
  25.    }  
  26.    .custom-slider .ngx-slider .ngx-slider-tick {  
  27.       width: 1px;  
  28.       height: 10px;  
  29.       margin-left: 4px;  
  30.       border-radius: 0;  
  31.       background: # ffe4d1;  
  32.         top: -1 px;  
  33.     }.custom - slider.ngx - slider.ngx - slider - tick.ngx - slider - selected {  
  34.         background: orange;  
  35.     }  
  36. }  
 

Vertical slider

 
The vertical slider mostly changes the angle with vertical position. Here we need to set options; vertical is true and changes will be reflected in the screen.
  1. value: number = 50;  
  2. options: Options = {  
  3.     floor: 0,  
  4.     ceil: 100,  
  5.     step: 10,  
  6.     vertical: true  
  7. };   
  8. <div style="height: 150px;">  
  9.    <ngx-slider [(value)]="value" [options]="options"></ngx-slider>  
  10. </div>  
 
I hope this article is most helpful for you.


Similar Articles