I have a sidebar Angular component which uses a local script sidebar.js. In src\app\sidebar\sidebar.component.ts, I tried loading sidebar.js as follows:
ngAfterViewInit(): void {
const s = document.createElement('script');
s.type = 'text/javascript';
s.src = 'src/app/sidebar/sidebar.js';
console.log('creating external script: ' + s.src);
this.elementRef.nativeElement.appendChild(s);
}
I got this error:
http://localhost:4201/src/app/sidebar/sidebar.js net::ERR_ABORTED 404 (Not Found)
Yes, I have this line in angular.json as well:
"scripts": [
...
"src/app/sidebar/sidebar.js"
]
And the following also failed with a 404:
s.src = 'sidebar.js';
Unfortunately since sidebar component resides in an Angular module that I am building for a customer, I cannot put sidebar.js in /assets as suggested in My Local JavaScript file is not loaded in Angular 2 . The /assets directory resides in my customer's code. Also, sidebar.js contains this logic :
const _R = document.querySelector('[type=range]');
_R.style.setProperty('--val', +_R.value);
_R.style.setProperty('--max', +_R.max);
_R.style.setProperty('--min', +_R.min);
which depends on sidebar.component (which resides in my module):
<input
class="border-0 slider"
type="range"
min="10"
[title]="scale"
step="5"
[(ngModel)]="scale"
max="200"
orient="vertical"
list="tickmarks"
/>
Yes, I realize many similiar questions were asked and answered on SO:
angular2: including thirdparty js scripts in component
script tag in angular2 template / hook when template dom is loaded
but none of them address the aspect that sidebar.js and index.html are in two different modules.