Script file on an angular Component

Asked

Viewed 681 times

0

Hello, I have an Angular System that requires a specific script!

This Component is not started on the first screen of my application, so I cannot put my script file in the Bundler, because by the Bundler this file is loaded only once it is the startup of the first compoenent that will be loaded. For example:

In my 3rd place I have an id input 'data', and in this jquery file I have an id called like this: $('#data'). When the bundler goes up this file, it goes up in the 1st Component only and when it arrives in the 3rd it does not load again, making it impossible to read my Id.

I wonder if there is a way to call this specific file only when my Component is called, so for example:

<script src="../../../../scripts/teste.js"></script>

<div class="container">
    <p class="interest-preference">Escolha uma data e horário.</p>
    
    <!--adjust Form Position-->
    <div class="clear"></div>
    
    <div class="wrap-input">
        <input class="date-picker input.validator" id="datepicker" name="datepicker" type="text" placeholder="Data">
        <label class="label-input" for="data">
            <span class="far fa-calendar-alt"></span>
        </label>
    </div>
    
    <div class="wrap-input">
        <input class="timepicker" type="text" name="horario" id="horario" placeholder="Horário">
        <label class="label-input" for="horario">
            <span class="far fa-clock"></span>
        </label>
    </div>
    
    <div class="wrap-input">
        <div class="wrap-btn">
            <button class="btn">
                CONFIRMAR
            </button>
        </div>
    </div>
</div>

Error presented

inserir a descrição da imagem aqui

1 answer

1


I made this generic method to add a js dynamically. See that method and asynchronous. So just use the script in callback.

constructor(private renderer: Renderer2) {
}

addJsToElement(src: string): HTMLScriptElement {
    const script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = src;
    this.renderer.appendChild(document.body, script);
    return script;
  }

call the method so:

this.addJsToElement('https://widgets.skyscanner.net/widget-server/js/loader.js').onload = () => {
        console.log('SkyScanner Tag loaded');
}

STACKBLITZ

  • I can replace this link passed as a parameter by a local file path? for example: this.addJsToElement('.. /.. /.. /scripts/test.js')....

  • tries to place the file inside the Assets folder

  • and the way is the same way you use things from Assets normally "Assets/js/Myfile.js"

  • I put the call in the method: 'ngOnInit()' and it didn’t work. put it this way: ngOnInit(): void { this.formulario = this.construirFormGroup(this.inputs); this.addJsToElement('../..../scripts/animacao.js'). onload = () => { console.log('Skyscanner Tag Loaded'); } }

  • puts the script inside the Assets folder

  • I put it on, and it still worked...

  • Ta calling so this.addJsToElement('Assets/scripts/animacao.js')

  • I edited my answer

  • This answer link is presenting a walk online. a link. I need a physical path. I tried applying the path to the Assets folder and it still didn’t work. I will edit my question and add a photo showing the error shown.

  • take a look at what I edited for Body.

  • I get it, I’m gonna test

  • i edited my code a look there at the penultimate line of the addJsToElement method.

  • That worked 100% now. can insert as reply I give the like here, thank you

  • What do you mean? I don’t understand.

Show 9 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.