Search code examples
angularaspnetboilerplatejquery-knob

Add jQuery-Knob in Angular 2


I am new to Angular 2.0 and using aspnetboilerplate template.

I am trying to integrate jQuery-Knob.

First, I included jquery.knob.min.js in scripts in .angular-cli.json:

"scripts": 
[
    "../src/bsb-theme/js/jquery.validate.js",
    "../src/bsb-theme/js/jquery.knob.min.js"
]

Then, I made an input in dashboard.component.html:

<input type="text" class="knob" value="40"
    data-width="125"
    data-height="125"
    data-thickness="0.25"
    data-angleArc="250"
    data-angleoffset="-35"
    data-fgColor="#00BCD4">

But the output is a text box only.


Solution

  • jQuery-Knob doesn't work out-of-the-box with Angular 2.

    Try this library: https://github.com/xzegga/angular2-knob


    Usage

    IMPORTING ANGULAR MODULE:

    import { KnobModule } from "angular2-knob";
    
    @NgModule({
      declarations: [
        AppComponent
      ],
      imports: [KnobModule]
      bootstrap: [AppComponent]
    })
    

    HTML USE:

    <div ui-knob [value]="value" [options]="knOptions"></div>
    

    CONFIGURING OPTIONS IN ANGULAR COMPONENT:

    import { Component, OnInit } from '@angular/core';
    
    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css']
    })
    export class AppComponent{
      knOptions = {
        readOnly: true,
        size: 140,
        unit: '%',
        textColor: '#000000',
        fontSize: '32',
        fontWeigth: '700',
        fontFamily: 'Roboto',
        valueformat: 'percent',
        value: 0,
        max: 100
        trackWidth: 19,
        barWidth: 20,
        trackColor: '#D8D8D8',
        barColor: '#FF6F17',
        subText: {
          enabled: true,
          fontFamily: 'Verdana',
          font: '14',
          fontWeight: 'bold',
          text: 'Overall',
          color: '#000000',
          offset: 7
        },
      }
      value = 45; 
    }