Angular Scripts

Asked

Viewed 464 times

0

Hello, I have an angular application where I am using jquery scripts to implement a datapiker and a timepiker, these presented below:

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

I have a separate script for their startup, presented below:

 jQuery(document).ready(function () {
            $('#datepicker, .datepicker').datepicker({ dateFormat: 'dd.mm.y' });
            $('.select').styler();
            $('input.timepicker').timepicker({ timeFormat: 'HH:00' });

            $('.radio-list__input-wrap').mousedown(function () {
                changeCheck($(this));
            });
            $('.radio-list__input-wrap').each(function () {
                changeCheckStart($(this));
            });


            function changeCheck(el) {
                var el = el, input = el.find('input').eq(0);
                if (!input.attr('checked')) {
                    $('.radio-list__input-wrap').each(function () {
                        cInput = $(this).find('input').eq(0);

                        if (cInput.attr('name') == input.attr('name')) {
                            $(this).removeClass('radio-list__item_active');
                            cInput.attr("checked", false);
                        }
                    });
                    el.addClass('radio-list__item_active');
                    input.attr("checked", true);
                }
                return true;
            }
            function changeCheckStart(el) {
                var el = el, input = el.find('input').eq(0);
                if (input.attr('checked')) {
                    el.addClass('active');
                }
                return true;
            }
        });

However this screen is not my home screen, with this the datapiker and the timepiker is only presented if der F5 on this screen presents, so the script would load them.

I would like to know how to load this script as soon as this page is presented?

Thank you in advance

  • I recommend you use another date Picker library built pro angular.

  • In the company where I work the scripts is already ready, I can not replace...

1 answer

2


According to the angular-cli documentation you can install by npm normally and add the angular-cli.json scripts tag.

npm install jquery — save

angular-cli.json

“scripts”: [ “../node_modules/jquery/dist/jquery.min.js” ]

In its component

import * as $ from ‘jquery’;

References: https://github.com/angular/angular-cli/wiki/stories-global-scripts

To add the jquery type for autocomplete: https://github.com/angular/angular-cli/wiki/stories-third-party-lib

EDIT

To add a javascript dynamically in an angular Component I made this function that takes as parameter the string of the script (usually a Cdn). For example:

https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js

import { Renderer2 } from '@angular/core'; 

constructor(private renderer: Renderer2) {}

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

And you can call her that:

this.addJsToElement('https://widgets.skyscanner.net/widget-server/js/loader.js').onload = () => {
        console.log('SkyScanner Tag loaded');
}
  • So the problem is that it is not loading because when startar the application, the jquery scripts are read only once. So when it arrives on the third page it no longer loads, not loading also the datapiker and the timepiker...

  • you have to put this code using jquery on the third page. And do not need the ready Document, you can use another angular Lifecycle hook.

  • Once jquery is loaded it is not downloaded.

  • I tried to add the code directly on the third page and it still didn’t roll! : ( ?

  • Is there any way you can edit your question so I can see how you’re calling this code at the angle? It has to load a javascript dynamically but a little more complex. I will edit my answer.

Browser other questions tagged

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