Search code examples
javascriptangularnicescroll

Init external plugin in Angular (4) app


how can I use external plugin in angular 4 app? I need use niceScroll plugin for scrollable div, which is represented by component.

<div class="d-flex" id="scrollable">
    <app-threads-list></app-threads-list>
</div>

I call nicescroll init function on element with id scrollable, from component threads-list like that:

$('#scrollable').niceScroll();

Solution

  • I guess you are trying to use jquery in Angular .

    Without typings Step One

    npm install jquery --save 
    

    Step two

    Add the jquery scripts file in .angular-cli.json file
    
    
    "scripts": [ "../node_modules/jquery/src/jquery-min.js" ]
    

    Step three

    Adding a var in component to be used as a global variable
    

    //using external js modules in Angular

    declare var $: any;
    
    
    Then in ngAfterViewInit(){
      $('#scrollable').niceScroll();
    }
    

    Check this link for more info LINK with and without typings