By adding them to the scripts property of .angular-cli file, one can load global scripts into your app. This example comes from the documentation:
\
If you configure the "lazy" attribute in the .angular-cli.json
to load global libraries, you need to "lazy load" the script when needed. Here is the steps to setup.
1.Configure .angular-cli.json
in the apps[0].scripts
array.
"scripts": [
{ "input": "../node_modules/jquery/dist/jquery.js", "output": "jquery", "lazy": true }
],
You'll get an
jquery.bundle.js
file in the build output.
2.Load the generated script by appending tag in the DOM (
) lazily.
const script = document.createElement('script');
script.src = 'jquery.bundle.js';
script.type = 'text/javascript';
script.async = true;
script.charset = 'utf-8';
document.getElementsByTagName('head')[0].appendChild(script);