2

I have added jQuery to my angular 4 project, but I am having trouble using scripts designed to use jQuery.

jQuery was installed as an npm package then was added via angulars cli using the typings command

I would like to access those jQuery scripts in my compontents.ts files.

The script in question is: this

2 Answers 2

1

in your angular-cli.json:

"scripts": [
  "../node_modules/jquery/dist/jquery.min.js"
],

and in your component:

import * as $ from 'jquery';
Sign up to request clarification or add additional context in comments.

7 Comments

Sorry, but as states in the question, I already have jQuery inplemented via typings, what I am after is how to implement a plugin for jQuery (scrollbox), so I would be able to call $("#demo").scrollbox() as per the examples on scrollbars github
So normally, what I wrote works. $("#demo") will work. I do that in my project.
I know` $("#demo")` works but I need the last part .scrollbox() to work.
It doesnt work with import * as scrollbox from 'jquery.scrollbar' ?
it doesnt and hence I am asking here, but man I dont get it why people are telling me how to setup jQuery when I literally asked how to add a jQuery script/plugin
|
0

in your component you have to declare a global variable for jquery selector like that :

import ... 

declare var $: any;

@Component({
 ...
}) 
export class ANY_CLASS implements OnInit{
   ngOnInit() {
     $('#demo').scrollbox();
  }
//use it as you like after that
}

and in your template :

<div id="demo" class="scroll-text">
  <ul>
    <li>item 1</li>
    <li>item 2</li>
    <li>item 3</li>
  </ul>
</div>

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.