0

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.

6
  • If you want to use Angular 4 than you must be convert into typescript.I don't think it is possible without typescript Commented Mar 29, 2018 at 6:53
  • You can always use native javascript in typescript Commented Mar 29, 2018 at 6:54
  • 1
    You don't have to use Typescript at all. You can build your Angular application with JS alone. nicholasjohnson.com/blog/… Commented Mar 29, 2018 at 6:55
  • 2
    TypeScript is a superset of JavaScript, so any JavaScript is valid TypeScript. Commented Mar 29, 2018 at 6:55
  • take a look at this might help you stackoverflow.com/a/46008402/2708210. Commented Mar 29, 2018 at 7:02

1 Answer 1

1

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;
      }
    }
});

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.