Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind <script> element's src attribute in AngularJS

Tags:

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.

like image 796
Arsen Y.M. Avatar asked Dec 05 '14 00:12

Arsen Y.M.


People also ask

What is script src in AngularJS?

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>

How to use ng src in AngularJS?

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.

Does ng bind bind the application data to HTML tags in AngularJS?

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.

What is $$ in AngularJS?

The $ in AngularJs is a built-in object.It contains application data and methods.


1 Answers

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.

like image 119
Kolban Avatar answered Oct 23 '22 14:10

Kolban