I am trying to bind the src
attribute of an HTML <script>
element to a variable in my angular controller, so that I can update it from controller without dealing with any UI.
So far I've tried all these options:
<script type="text/javascript" ng-src="{{sourceUrl}}"></script> <script type="text/javascript" src="{{sourceUrl}}"></script> <script type="text/javascript" ng-src="sourceUrl"></script>
In my controller I have:
$scope.sourceUrl = "https://<some url goes here>";
When running the page in the browser after the $scope.sourceUrl gets set there is no outgoing request to sourceUrl, so I am sure I am doing something wrong. Any ideas?
I found several posts about src
attribute of <img>
element, and ng-src
should work as they say, but I guess <script>
is somehow different.
AngularJS is a JavaScript framework written in JavaScript. AngularJS is distributed as a JavaScript file, and can be added to a web page with a script tag: <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
AngularJS ng-src Directive The ng-src directive overrides the original src attribute of an <img> element. The ng-src directive should be used instead of src if you have AngularJS code inside the src value. The ng-src directive makes sure the image is not displayed wrong before AngularJS has evaluated the code.
ng-bind directive binds the AngularJS Application data to HTML tags. ng-bind updates the model created by ng-model directive to be displayed in the html tag whenever user input something in the control or updates the html control's data when model data is updated by controller.
The $ in AngularJs is a built-in object.It contains application data and methods.
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 <script>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With