AB_Krish
AB_Krish

Reputation: 15

How to use Javascript in Angular 4

Hi Have a Bootstrap navbar component with javascript like :

https://bootsnipp.com/snippets/a35lo

In above example, It has HTML, CSS, and JS, If I want to use above in Angular 4, How can I use above JS directly instead of converting it into typescript.

Upvotes: 0

Views: 108

Answers (1)

Parth Savadiya
Parth Savadiya

Reputation: 1211

First you need to load js file using scriptLoaderService in your component. then you can use any JQuery or third-party library code in your ts file.

(<any>$('body')).scrollspy({
  target: '#navbar-collapsible',
  offset: 50
});

/* smooth scrolling sections */
(<any>$('a[href*=#]:not([href=#])')).click(function() {
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
      var target = $(this.hash);
      target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
      if (target.length) {
        $('html,body').animate({
          scrollTop: target.offset().top - 50
        }, 1000);
        return false;
      }
    }
});

Upvotes: 1

Related Questions