1

i try to import the jquery-knob plugin in my Angular project, so i installed jquery via npm – that works fine. Now i want to add the jquery-knob (also via npm) but i get this error, i think its in the

jquery.knob.min.js

Uncaught ReferenceError: jQuery is not defined
    at eval (eval at webpackJsonp.310.module.exports (addScript.js:9), <anonymous>:1:85)
    at eval (eval at webpackJsonp.310.module.exports (addScript.js:9), <anonymous>:1:95)
    at eval (<anonymous>)
    at webpackJsonp.310.module.exports (addScript.js:9)
    at Object.155 (jquery.knob.min.js?4d31:1)
    at __webpack_require__ (bootstrap aa173b5…:52)
    at Object.351 (addScript.js:10)
    at __webpack_require__ (bootstrap aa173b5…:52)
    at webpackJsonpCallback (bootstrap aa173b5…:23)
    at scripts.bundle.js:1 

in .angluar-cli.json

"scripts": [
        "../node_modules/jquery-knob"
      ],

component:

import * as jQuery from 'jquery';
import * as knobObj from 'jquery-knob';

How can I pass the JQuery Instance to the jquery-knob plugin?

Used Version:

@angular/cli: 1.0.1
node: 6.10.0
os: darwin x64
@angular/common: 4.1.0
@angular/compiler: 4.1.0
@angular/core: 4.1.0
@angular/forms: 4.1.0
@angular/http: 4.1.0
@angular/platform-browser: 4.1.0
@angular/platform-browser-dynamic: 4.1.0
@angular/router: 4.1.0
@angular/cli: 1.0.1
@angular/compiler-cli: 4.1.0

2 Answers 2

1

Now i found a solution, maybe it helps somebody :-), I put the knob in a own component and create a native Element, then pass the values between with input() and output(), see the code:

myknob.component

import {Component, ElementRef, EventEmitter, Input, OnInit, Output} from '@angular/core';
import * as $ from 'jquery';
import "jquery-knob";

@Component({
  selector: 'app-myknob',
  templateUrl: './myknob.component.html',
  styleUrls: ['./myknob.component.css']
})
export class MyknobComponent implements OnInit {

  knobElement: any;

  @Input() sliderVal : number;
  @Output() sliderValChange = new EventEmitter<number>();

  constructor(private elementRef : ElementRef) { }

  ngOnInit() {
    this.knobElement = $(this.elementRef.nativeElement);
    this.knobElement.knob({
        "bgColor": "#FFF",
        "fgColor": "#222222",
        "thickness" : .2,
      change : (value) => this.setVal(value)
    });
    this.knobElement.val(this.sliderVal).trigger('change');


  }

  setVal(val) {

    this.sliderVal = val;
    this.sliderValChange.emit(this.sliderVal);
  }

}

myknob template:

<input type="text"  [attr.value]="sliderVal">

the parent component template

<app-myknob class="myKnob" [sliderVal]="inputVal" (sliderValChange)="setSliderVal($event)"></app-myknob>

the parent component

setSliderVal(sliderVal: number) {
    console.log(sliderVal);
  }
Sign up to request clarification or add additional context in comments.

Comments

0

Use angular2-knob instead jquery plugin, this package has been re-written completely for angular 4 using d3.js V4

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.