6

I try to use Jquery-ui slider with angular2. I would like to have the variable "slideValue" displaying the value of the slider but I can't figure out how to bind my model or a variable from angular to the slider. Here is my slider component:

import {Component, ElementRef, Inject, OnInit} from 'angular2/core';

declare var jQuery:any;

@Component({
    selector: 'slider',
    template: 
    `
    <input type="text" [(ngModel)]="slideValue" id="amount" required placeholder="Enter a name here">
    <div id="slider"></div>
    <h2>slideValue = {{slideValue}}</h2>
    `
})

export class Slider implements OnInit {
    elementRef: ElementRef;
    slideValue: number;

    constructor(@Inject(ElementRef) elementRef: ElementRef) {
        this.elementRef = elementRef;
    }

    ngOnInit() {        
        jQuery(this.elementRef.nativeElement).find("#slider").slider({
          range: false,
          orientation: "vertical",
          min: 0,
          max: 100,
          value: 60,
          slide: function( event, ui ) {
            this.slideValue = ui.value; //doesn't seem to work
            $( "#amount" ).val( ui.value ); 
          }
        });

    }
}

The code is available here:

https://github.com/nerg/slider

I would like to be able to use any "vertical slider" with Angular2, so if another viable solution exists, I'm interested (i've check material but it doesn't seem to be "angular2" ready and only horizontal slider).

3
  • See PrimeNG Slider as a reference. primefaces.org/primeng/#/slider Commented Feb 16, 2016 at 17:32
  • That exactly what I was looking for. I search for a while but never find this page. Thanks. Commented Feb 17, 2016 at 13:44
  • As of the current ng2 forms 0.2.0 release (corresponding to rc4), the slider's value property is required to be defined (null is ok), whereas previous ng2 forms versions would also allow the value to be undefined. This came up when I was migrating a PrimeNG ng2 demo app to rc4 to see what broke, and starting to drag a slider with an undefined initial value started throwing exceptions. Commented Jul 20, 2016 at 18:56

2 Answers 2

6

You need to use correct context (this) inside of slide callback. Arrow function will do the trick in this case:

@Component({
    selector: 'slider',
    template: `
        <input type="text" [(ngModel)]="slideValue" class="amount" required placeholder="Enter a name here">
        <div class="slider"></div>
        <h2>slideValue = {{slideValue}}</h2>
    `
})
export class Slider implements OnInit {
    elementRef: ElementRef;
    slideValue: number;

    constructor(@Inject(ElementRef) elementRef: ElementRef) {
        this.elementRef = elementRef;
    }

    ngOnInit() {
        var $amount = jQuery(this.elementRef.nativeElement).find(".amount");
        jQuery(this.elementRef.nativeElement).find(".slider").slider({
            range: false,
            orientation: "vertical",
            min: 0,
            max: 100,
            value: 60,
            slide: (event, ui) => {
                this.slideValue = ui.value;
                $amount.val(ui.value);
            }
        });
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

I was looking for something similar and came across this:

https://www.npmjs.com/package/ng2-slider-component

Not 100% polished and github activity behind it looks thin, but closest thing to a native angular2 component that seems to exist at the time of this writing.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.