I'm trying to use a jquery slider library in my Angular2 application. The problem is that I dynamically add new sliders and I must call a function to make the library display the slider correctly.
$( ".slider" ).each(function( index ) {
$(this).slider();
$(this).slider('setValue', $(this).attr("data-slider-value"))
});
For the moment I call it whenever a new element is added, but the problem is that I call it before Angular2 actually updates the html.
setTimeout(() => {
$( ".slider" ).each(function( index ) {
$(this).slider();
$(this).slider('setValue', $(this).attr("data-slider-value"))
});
}, 100);
But it's not very clean.
I'd like a way to call a function from the HTML when it is added to the page. Something like :
<slider (onLoad)="loadSlider()"></slider>
Is it possible in Angular2? Or is there a better way to do this?
SOLUTION
I got it working with Gilsdav answer.
form.component.html
<app-slider [min]="field.min" [max]="field.max" [value]="field.min"></app-slider>
slider.component.ts
import {Component, ViewChild, Input} from '@angular/core';
declare var $ : any;
@Component({
selector: 'app-slider',
templateUrl: './slider.component.html',
styleUrls: ['./slider.component.css']
})
export class SliderComponent{
@ViewChild('mySlider') slider: any; // can be ElementRef;
@Input() min: number;
@Input() max: number;
@Input() value: number;
constructor() { }
ngAfterViewInit() {
// slider is available
$(this.slider.nativeElement).slider();
let value = $(this.slider.nativeElement).attr("data-slider-value");
$(this.slider.nativeElement).slider('setValue', value);
}
}
slider.component.html
<input #mySlider
class="slider"
type="text"
name="slider"
data-provide="slider"
data-slider-min="1"
data-slider-max="3"
[attr.data-slider-min]="min"
[attr.data-slider-max]="max"
data-slider-step="1"
[attr.data-slider-value]="value"
data-slider-tooltip="show"/>
But I have an error, which didn't occure when using Jquery. this.slider.slider is not a function