Is there anyway that we can append a script file in the index.html after we build our angular app?
things that i have tried:
{
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": { ... },
"configurations": {
"production": {
"fileReplacements": [{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}],
"scripts": ["src/assets/scripts/my-custom-script.js"],
...
}
}
}
}
and this:
// main.ts
if (environment.production) {
enableProdMode();
const scriptEl = window.document.createElement('script');
scriptEl.src = 'https://js.intercomcdn.com/vendors-app-modern.28279aba.js';
window.document.body.appendChild(scriptEl);
}
in both proposed solutions my dist/projectName/index.html file remained unchanged after i ran npm run build command...
You can add the script directly in the index.html
UPDATED
In my opinion, you have one option :
Create a folder named dynamic_script in the
assets
and then put 2 files there1- Your js file (like ARRS5125-MLL-YourJsfile.js)
2- A JSON file (like info.json), set the name of your file here
NOTE
Whenever the name of your script is changed, change the filename in the JSON file based on your js name.
Now, You can dynamically load your js file as below
Step 1: Create a service :
Step 2: In you component
NOTE:
After building your project whenever you add the new js file in the dynamic_script then open the
info.json
and replace the new name manually.