Good day everyone!
My task is to load two external modules in one component. I load external scripts through directives rendering. The problem is that only the last one is loaded, the first one is "overwritten" at boot. I see that the whole thing is in rendering when I create a new element. How to fix it?
home.component.html
<div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="nav-home-tab"> <searchTourSimple [script]="'src/app/scripts/homeSearch.js'"></searchTourSimple> </div> <div class="shop-window"> <div id="shopwindow-container"> <shopWindow [script]="'src/app/scripts/shopwindow.js'"></shopWindow> </div> </div> dir1.directive.ts
@Directive({ selector: 'searchTourSimple' }) export class SearchModuleSimpleDirective implements OnInit { @Input('script') param: any script: any constructor(private renderer:Renderer2) { } ngOnInit() { this.script = this.renderer.createElement('script') this.script.type = 'text/javascript' this.script.src = this.param this.script.async = true this.renderer.appendChild(document.head, this.script) document.write = function(input: string) { document.getElementById('home').innerHTML += input } } } dir2.directive.ts
@Directive({ selector: 'shopWindow' }) export class ShopWindowDirective implements OnInit { @Input('script') param: any script: any constructor(private renderer:Renderer2) { } ngOnInit() { this.script = this.renderer.createElement('script') this.script.type = 'text/javascript' this.script.src = this.param this.script.async = true this.renderer.appendChild(document.head, this.script) document.write = function(input: string) { document.getElementById('shopwindow-container').innerHTML += input } } }
document.write = function(input: string)? - overthesanityinputis a module if it is a string type parameter? - overthesanity