Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <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>
- @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
- }
- }
- }
- @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
- }
- }
- }
- import { Injectable } from '@angular/core';
- import { Observable, of } from 'rxjs';
- import { tap, switchMap } from 'rxjs/operators';
- @Injectable({ providedIn: 'root' })
- export class ScriptService {
- private loadResource(src: string): Observable<void> {
- return new Observable((observer) => {
- const script = document.createElement('script');
- script.async = true;
- script.src = src;
- script.addEventListener('load', () => {
- observer.next();
- observer.complete();
- });
- document.head.appendChild(script);
- });
- }
- private patchDocumentWrite(id: string): void {
- document.write = (input: string): void => {
- document.getElementById(id).innerHTML += input;
- };
- }
- public loadExternalResources(): Observable<void> {
- return of(null).pipe(
- tap(() => this.patchDocumentWrite('home')),
- switchMap(() => this.loadResource('src/app/scripts/homeSearch.js')),
- tap(() => this.patchDocumentWrite('shopwindow-container')),
- switchMap(() => this.loadResource('src/app/scripts/shopwindow.js'))
- );
- }
- }
- export class AppComponent implements AfterViewInit {
- constructor(private scriptService: ScriptService) {}
- public ngAfterViewInit(): void {
- this.scriptService.loadExternalResources().subscribe(() => {
- console.log('все скрипты загрузились');
- });
- }
- }
Add Comment
Please, Sign In to add comment