0

I have to use jQuery-Knob in a custom component. After a lot of search and try/miss attempts the only way in how I was able to init my Knob was wrapping the jQuery function inside a setTimeout with 0 seconds like this answer says.

constructor() {
    console.log('Hello SsdKnob Component');

    setTimeout(() => {
        $(".dial").knob();
    }, 0)
}

But if feels kind of hacky and maybe inestable? (I not sure if I will have problems because of some kind of racing conditions).

Is there a better/recommended way to do this?

I have tried without success:

ionViewLoaded() {
     $(".dial").knob();

}

and

AfterViewInit() {
    $(".dial").knob();
}
Community
  • 1
  • 1
distante
  • 6,438
  • 6
  • 48
  • 90

1 Answers1

6

AfterViewInit() { should be

ngAfterViewInit() {

alternatively you can also use

ngAfterContentInit() {

which is run after the view and projected content is ready.

If jQuery-Knob does some async initialization, this might not be enough. Then you should check if the component emits an event when it's done initializing itself. setTimeout() should be the last resort.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567