Angular provides ways to dynamically load templates with dynamic names via ng-include
. Inline JS and CSS in that partial will load fine, but there is not a good way to download scripts with dynamic urls. We needed to download scripts, relative to the path of the .html partial calling them. (i.e. we had a directory with a file to include, and wanted the .html
file to declare the scripts etc. it needed itself).
As opposed to AngularJS: How to make angular load script inside ng-include?, which is loading a script with a static src in a dynamically included partial, I want to include dynamically create the src for a script in a dynamically included partial, using either interpolation or running it through and angular function.
For example, I may be dynamically loading a partial into my app, from a directory in which there are several other resources that partial relies on. Instead of downloading all the files individually, I want to leave that logic in the partial. I realize that the browser cannot do this, so I'll use ng-src
and allow Angular to do the heavy lifting here. Instead of parsing every script src tag relative to the partial, so I'm going to use a function to generate the urls, like so:
<script type="application/javascript" ng-src="prefixUrl('myUrl.js')"></script>
How do I do it?