DavidL DavidL - 1 year ago 349
AngularJS Question

Angular 2 Execute JS from file using AfterViewInit

Quite new to Angular 2, and after looking around for few hours I'd like to have some help.

I have a JS file with some generic functions. For example:


This file contains in fact way more code. As you can imagine, I'd like to enable tooltips of all components. I can't simply include this file in
because subcomponents aren't present (yet) when the file is loaded.

After some digging,
came up. This post suggests to copy/paste JS code into
. That's the ugly way (in my opinion)...

So here I come with 2 related questions:

1# Is there a way to execute JS code when a child component is loaded? For example, something like:

// In app.component.ts
// JS code here

This solution is quite interesting because I'm not forced to implement
with the same content in all my components. But is it possible?

2# Is there a way to import JS code instrad of copy/paste it into
? I don't want copy/paste 300 lines of JS code into 15 differents components (for obvious reasons).

Thanks a lot for your help!

Answer Source

Got a less-ugly solution, if someone has a better one I'll gladly accept his answer!

      $.getScript('assets/js/myscript.js', function(){});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download