while changing the routes to homepage, javascript and jquery code is not loading in Angular 5
first, add your script to a .js file and save it under angular.json like below
"scripts": ["src/assets/js/modelsupport.js"],
add below code in you component.ts that you want it to work
url could start with src/assets or assets according to your angular version
url = "assets/js/modelsupport.js";
after that include below code in ngOnInit() or where ever you want to call
this.loadAPI = new Promise(resolve => {
console.log("resolving promise...");
this.loadScript();
});
after that add loadScript() function in you class
public loadScript() {
console.log("preparing to load...");
let node = document.createElement("script");
node.src = this.url;
node.type = "text/javascript";
node.async = true;
node.charset = "utf-8";
document.getElementsByTagName("head")[0].appendChild(node);
}
Hope you could solve your problem
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With