Gaetan
Gaetan

Reputation: 488

Manually load and unload external javascript in AngularJS

Is it possible to tell angular to load an external javascript when displaying a view and unload it when the view is left?

I have potentially 2 incompatibles javascript libraries: highcharts and highstocks. Highstocks provides highcharts but without some feature (no 3D,...). Using Highcharts-ng only works when highstocks.js is loaded.

But for some other views, I would like to use highcharts.js on some specific graph.

So, at worst, I would like to have some angular views that displays highcharts (loading highcharts.js), some other that displays highstocks. But since these external javascripts needs to be loaded in the index.html, this cannot work.

Is it possible to load highcharts.js in a view, unload it when leaving this view, load highstocks.js when switching to another view?

Thanks

Upvotes: 1

Views: 786

Answers (1)

Norbor Illig
Norbor Illig

Reputation: 658

I'd recommnd you to use Angularjs together with Requirejs(http://requirejs.org)

Although Angularjs does not come with any built in AMD functionality, there are some third party modules available on github to help you do that. I use angularAMD (https://github.com/marcoslin/angularAMD), but there are other options like ocLazyLoad (https://github.com/ocombe/ocLazyLoad), and others.

Upvotes: 1

Related Questions