ng2-nouislider

repository·master·Indexed 41 days ago

https://github.com/tb/ng2-nouislider

An Angular component wrapper for the noUiSlider library, version 2.0.0. It provides integration with Angular's template-driven (ngModel) and reactive forms via ControlValueAccessor. Requires Angular 14 or higher. The NouisliderComponent supports various configuration inputs such as min, max, step, and behaviour, and emits events like change, update, and slide.

Tokens
1.7K
Snippets
8
Records
11
Agent score
52%

What's inside ng2-nouislider

  1. How NouiFormatter works

    master

    The NouiFormatter interface is used to bridge the gap between the raw string values used by noUiSlider and the numeric values used by your Angular application.

    By default, the component uses DefaultFormatter, which handles basic float parsing and fixed-point string conversion. If you need specific currency symbols, units (like % or px), or specific decimal precision, you should provide your own implementation via the [format] input on the NouisliderComponent or within the [config] object.

  2. Use the NouisliderComponent

    master

    The NouisliderComponent is an Angular wrapper for noUiSlider. It can be used as a standalone component and supports both template-driven and reactive forms via ControlValueAccessor.

    Basic Configuration

    You can configure the slider using various @Input properties that map to noUiSlider options. Common inputs include:

    • min / max: The range boundaries.
    • step: The step size.
    • range: An object defining the range (e.g., { min: 0, max: 100 }).
    • connect: Boolean or array to connect the handles to the bar.
    • behaviour: String defining slider behavior (e.g., 'mixed', 'range').
    • snap: Boolean to enable snapping to steps.
    • disabled: Boolean to disable the slider.

    Event Outputs

    The component emits several events based on slider interactions:

    • change: Emitted when the slider value is changed (usually on release).
    • update: Emitted when the slider value is updated (during sliding).
    • slide: Emitted during sliding.
    • start: Emitted when the user starts interacting with the slider.
    • end: Emitted when the user stops interacting.
    • set: Emitted when the value is set.

    Note that emitted values are automatically converted from strings to numbers using the configured formatter.

    <nouislider
      [min]="0"
      [max]="100"
      [step]="1"
      [(ngModel)]="myValue"
      (change)="$event"
      (update)="$event">
    </nouislider>
  3. Import NouisliderModule into your Angular application

    master

    To use the NouisliderComponent within your Angular application, you must import the NouisliderModule into your component's module or standalone component imports array. This module exports the NouisliderComponent, making it available for use in your templates.

    import { NouisliderModule } from 'ng2-nouislider';
    
    @NgModule({
      imports: [
        NouisliderModule,
        // ... other imports
      ],
      // ...
    })
    export class AppModule { }
  4. Use nouislider within Reactive Forms

    master

    Integrate the slider with Angular Reactive Forms by using the [formControl] directive. This allows the slider to participate in form validation and state management.

    // In your component class
    this.form1 = this.formBuilder.group({ single: [10] });
    <!-- In your template -->
    <form [formGroup="form">
      <nouislider [min="0" [max="20" [step="0.5" [formControl="form.controls.single"></nouislider>
    </form>
  5. Implement a custom NouiFormatter

    master

    To control how values are converted between the slider's internal string representation and your application's numeric values, implement the NouiFormatter interface.

    • to(value: number): string: Converts a numeric value to a string for display on the slider.
    • from(value: string): number: Converts a string value from the slider back into a number for your application logic.
    import { NouiFormatter } from 'ng2-nouislider';
    
    export class MyCustomFormatter implements NouiFormatter {
      to(value: number): string {
        return `$${value.toFixed(2)}`;
      }
    
      from(value: string): number {
        return parseFloat(value.replace('$', ''));
      }
    }
  6. Configure NouisliderComponent Inputs

    master

    The NouisliderComponent accepts the following inputs for configuration:

    InputTypeDescription
    disabledbooleanDisables the slider.
    behaviourstringDefines the slider behavior (e.g., 'range', 'mixed').
    connectboolean[] or booleanConnects the handles to the slider bar.
    limitnumberThe limit for the slider.
    minnumberThe minimum value.
    maxnumberThe maximum value.
    snapbooleanEnables snapping to steps.
    animateboolean or boolean[]Enables animation.
    rangeanyThe range configuration object.
    stepnumberThe step size.
    formatNouiFormatterCustom formatter for value conversion.
    pageStepsnumberNumber of steps for page navigation.
    configanyA generic object for passing additional noUiSlider options.
    keyboardbooleanEnables keyboard interaction.
    onKeydownanyCustom keyboard event handler.
    tooltipsArray<any>Configuration for tooltips.