1
"styles": [
    "../node_modules/bootstrap/dist/css/bootstrap.min.css",
    "styles.css"
],
"scripts": [
    "../node_modules/jquery/dist/jquery.js",
    "../node_modules/bootstrap/dist/js/bootstrap.min.js"
]

I have added the styles and scripts in .angular-cli.json Everything is working fine.

I'm using some Jquery related code in index.html and I'm facing the below exception

Uncaught ReferenceError: $ is not defined
at (index):95

If I load the files using .angular-cli.json will it not reflect in my index.html?

Stackblitz example

I'm using animation.css and animation.js. Is it possible to load in .angular-cli.json file

AOS.init({
    easing: 'ease-in-out-sine',
    once:'true'
});
7
  • Can you provide an mvce? Commented Jun 1, 2018 at 10:47
  • try as follows, declare var $: any; Commented Jun 1, 2018 at 10:52
  • @AntoAntony Where I need to declare? Commented Jun 1, 2018 at 10:53
  • @vicbyte mvce means? Commented Jun 1, 2018 at 10:54
  • if you want to use it in a component, then you should declare outside the component class Commented Jun 1, 2018 at 10:55

3 Answers 3

1

If you add your jQuery script in .angular-cli.json, it'll be bundled and added just before the closing body tag, so probably just after your code. So when execution flow reaches your code, $ is not defined yet.

What you can do it using the windonw.onload event to execute jQuery code after all js files have been loaded

<body>
  <app-root></app-root>
</body>

<script>

  window.onload = function(){
    $('#abc').html('hello');
    }
</script>
</html>

I did not manage to get this to work on stackblitz, probably because of the way they load scripts. But it does work on a normal project

Note: You should not use jQuery in angular projects if you don't absolutely need it. If you just require it for bootstrap, then have a look at the ng-bootstrap library, which offers a angular-native implementation of bootstrap

Sign up to request clarification or add additional context in comments.

3 Comments

I'm using ngx-bootstrap library for components. JQuery is used for some animations.
Ok, I got confused because you are importing node_modules/bootstrap/dist/js/bootstrap.min.js
Thanks anyway!! I'm using AOS animation. Can I include that from node_modules. Can u pls check my edited question.
0

Inside your component.ts just declare as below

// Jquery declaration
declare var $: any;

@Component({
  selector: 'abc',
  templateUrl: './abc.html',
  styleUrls: ['./abc.scss']
})
export class ABCComponent implements OnInit{
}

1 Comment

Sorry. Not in the component. I need it in index.html
0

Install

npm i --save jquery

Declare

import {$,jQuery} from 'jquery';
window.$ = $;
window.jQuery = jQuery;

1 Comment

Where to add that

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.