In Angular, there are methods available to dynamically load templates with variable names using ng-include
. It's possible to include inline JS and CSS in the partial without any issues, but when it comes to downloading scripts with dynamic URLs, things get a bit tricky. Our requirement was to download scripts relative to the path of the .html partial that called them (for example, having a directory with the necessary files to include, and wanting the .html
file to specify its required scripts).
Unlike the scenario discussed in AngularJS: How to make angular load script inside ng-include?, where a static source script is loaded in a dynamically included partial, I am looking to dynamically create the source for a script within a dynamically included partial, either through interpolation or by executing it using an Angular function.
For instance, if I'm loading a partial into my application dynamically from a directory that contains additional resources the partial depends on, instead of downloading each file separately, I want that logic to reside within the partial itself. Knowing that the browser cannot handle this task, I'll utilize ng-src
and let Angular handle the heavy lifting. Instead of manually parsing every script src tag in relation to the partial, I'll define a function to generate the URLs as follows:
<script type="application/javascript" ng-src="prefixUrl('myUrl.js')"></script>
So how can I accomplish this effectively?