0

I am using angular 4 and i want use my JavaScript file to work globally and i have declared like this. index.html <script src="assets/js/jquery-1.11.1.min.js"></script> <script src="assets/js/bootstrap.min.js"></script>

it is working when i have to use them in app-component.html, but problem is that i have another project which as different folders for different pages it is not working therenot working here.

if use the same code in app.component.html it is working fine, but i have different pages within that app/layouts/projects/, here in projects it is not working when i declare js link in the index.html, it works fine in app folder but not working inside it's sub-folder

Hemanth
  • 136
  • 1
  • 1
  • 10
  • just add jquery and bootstrap to your package.json(webpack) and do npm i or npm install jquery and bootstrap. Then import it your component and use. – nithalqb Feb 05 '18 at 09:41
  • If you want to add your js https://stackoverflow.com/questions/44817349/how-to-include-external-js-file-in-angular-4-and-call-function-from-angular-to-j – Tony Feb 05 '18 at 09:43
  • If you want to add bootstrap and Jquery https://stackoverflow.com/questions/43557321/angular-4-how-to-include-bootstrap – Tony Feb 05 '18 at 09:46
  • thanks a lot, if i wan't to add any external JavaScript files what should i do like – Hemanth Feb 05 '18 at 09:47

1 Answers1

0

No need to add jquery directly in html file. You can do this:

1) npm install jquery --save
2) npm install @types/jquery --save
3) in your component : declare var $ :any; 
Tushar Nikam
  • 594
  • 4
  • 13
  • thanks a lot, if i wan't to add any external JavaScript files what should i do like – Hemanth Feb 05 '18 at 09:46
  • you can include extra script in angular-cli.json – Tushar Nikam Feb 05 '18 at 09:48
  • Don't include src in path. So path will be : "scripts": ["./assets/js/hello.js"] – Tushar Nikam Feb 05 '18 at 10:00
  • yeah i did in the same way, is there any option to declare in index.html and access it. – Hemanth Feb 05 '18 at 10:09
  • You can include in index.html file but you should not do it. What error you are getting while declaring in angular-cli ? – Tushar Nikam Feb 05 '18 at 10:10
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/164542/discussion-between-hemanth-and-tushar-nikam). – Hemanth Feb 05 '18 at 10:18
  • if use the same code in app.component.html it is working fine, but i have different pages within that app/layouts/projects/, here in projects it is not working when i declare js link in the index.html, it works fine in app folder but not working inside it's sub-folder – Hemanth Feb 05 '18 at 12:15