How can I load JavaScript on demand after a lightbox is displayed?

There is a button on every page of a site. When the button is clicked, a lightbox appears. The content of the lightbox is a form that relies on several JavaScript files including Angular. Those JavaScript files are used only for the form and nowhere else on the site.

In order to reduce page load time, my goal is to load the JavaScript files only after the user clicks the button.

Is there a best practice way of doing this? Some options I can think of are:

The content of the lightbox is an iframe and the sourced html
document contains script tags.

By understanding your question, You want to lazy-load your js/css/html . You can achieve this using two plugins

Require.js

Pros :

Explicit dependencies between modules, listed in code.

Asynchronous module loading.

Lazy (on-demand) module loading.

Support for one of the standard module wrappers (AMD), a lot of
community modules implement it, so your modules can depend on them.

Cons :

We can't inject the AngularJS modules on the fly. This loads files
only when needed i.e. RequireJS only loads javascript files but it doesn't register the new angular modules and components in this new code

OcLazyLoad

Pros :

OcLazyLoad is used to load the dependency files and also to inject
the AngularJS modules on the fly.

Loading Js and Css files are effortless.

Cons :

We can't maintain the dependency files structure.

So you can use Require.js to load dependencies between modules listed in code and OcLazyLoad to inject Angular module on fly . And for your question regarding angular itself on fly, I don't think you can do that in this case.

Email codedump link for How can I load JavaScript on demand after a lightbox is displayed?