2

https://github.com/ocombe/ocLazyLoad

I have used this for Angular 1, is there any alternative plugins like this to work for Angular 2? I want to include the 3rd party Javascript plugins on demand (lazy-load), into my Angular 2 project. So far no success.

I'm currently working on Angular CLI. I have tried to include in angular-cli.json file, including jquery seems work. But I think this is for global load.

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

Let's say I want a page with WOW.js plugin in that page. I should call a component with WOW.js initiated inside it, then the WOW.js file will be included dynamically. How to do that?

Thanks

1 Answer 1

0

You could add into your assets the WOW.js file in your angular-cli.json configuration file.

"assets": [
    "pathToYourPlugin/WOW.js"
  ],

Then into your component add a script tag to import your library.

<script src="/WOW.js"></script>
Sign up to request clarification or add additional context in comments.

6 Comments

my component html template is just a partial for entire page. is it ok if i put this <script src="/WOW.js"></script> between <body> and not in <head>?
Yes of course. The recommended way is at the end of the body. This answer is not really beaytiful but it solves your requirement.
<script src="assets/wow.js"></script> didn't import. I confirmed can see scripts here http://localhost:4200/assets/wow.js. when I do inspect the elements using dev tool, <script src="assets/wow.js"></script> tag is gone from html. please help. thanks
You should not add it through angular but through template or inner html.
Yes, I put it inside template dashboard.component.html file. @Component({ selector: 'app-dashboard', templateUrl: './dashboard.component.html' })
|

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.