0

I tried to use

<script type="text/javascript" ng-src="{{referenceDir.creative}}"></script>

to include a script in a ABSOLUTE file location that I stored in $scope.referenceDir.creative (which value is something like "/lib/creative.js")

I did a console.log in create.js, but I see no console.log been triggered where this piece of script is loaded in a template for a route ( I use ui-router btw).

I wonder am I using the wrong tag to reference a java-script file? These file suppose to only run on that specific route, because they are jquery files that associated with specific doms, which I do not want to include in other routes, that's why I include the script with ng-src INSIDE a template HTML.

Any hint I will be grateful. Thank you!

baao
  • 71,625
  • 17
  • 143
  • 203
ey dee ey em
  • 7,991
  • 14
  • 65
  • 121
  • Please refer.. http://stackoverflow.com/questions/27306706/how-to-bind-script-elements-src-attribute-in-angularjs – Janty Aug 02 '15 at 03:03
  • @Janty so basically, there is no way for angularjs to load specific script files for specific route's template and I have to use raw dir string? just wondering – ey dee ey em Aug 02 '15 at 03:04
  • Yes.. the script is only processed once and even then during page load and before Angular can ever get control. – Janty Aug 02 '15 at 03:05
  • @Janty ah.... so basically the script reference inside the template html is actually.... loaded, but not going to run, since js file run only on page LOAD. is that correct? – ey dee ey em Aug 02 '15 at 03:06

1 Answers1

-1

Unfortunately, you can not use Angular in this way. Angular processes the web page only after the page has been loaded and built by which time the tag has been processed its one time (script tags are only ever run once). Other tags such as img will change the visual appearance of the screen when their properties change after the page has loaded ... but as mentioned, the script is only processed once and even then during page load and before Angular can ever get control. by Kolban

Community
  • 1
  • 1
Janty
  • 1,708
  • 2
  • 15
  • 29
  • Is there any work around using other technologies (jquery, or some javascript inject into angularjs code) to get over this .. pitfall? – ey dee ey em Aug 02 '15 at 03:05
  • You can use "require.js" for dynamic js load. You can also develop your directive for script tag and solve. – Janty Aug 02 '15 at 03:07
  • I am not so familiar with requiredJS, would you kindly demostrate a quick demo on how to dynamically load a sample script within a angujarJS controller? Do we need to inject requiredJS as dependency? Please address it in a demo will be BEAUTIFUL! Thanks and sry to ask for extra work – ey dee ey em Aug 02 '15 at 03:11
  • Ok..will try also you can go through once for reference http://stackoverflow.com/questions/12197880/angularjs-how-to-make-angular-load-script-inside-ng-include for LAZY LOAD. – Janty Aug 02 '15 at 03:13
  • More help. http://www.sitepoint.com/using-requirejs-angularjs-applications/ https://www.startersquad.com/blog/angularjs-requirejs/ – Janty Aug 02 '15 at 03:16