如何绑定<脚本>在AngularJS元素的src属性 [英] How to bind <script> element's src attribute in AngularJS

查看:147
本文介绍了如何绑定<脚本>在AngularJS元素的src属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个HTML的的src 属性绑定<脚本> 元素中的一个变量我角控制器,这样我可以从控制器更新它不处理任何UI。

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>

在我的控制器我有:

$scope.sourceUrl = "https://<some url goes here>";

运行页面当浏览器后,$ scope.sourceUrl获取设置有对sourceUrl没有传出的要求,所以我相信我做错了什么。任何想法?

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?

我发现关于的src 属性&LT几个职位; IMG&GT; 元素, NG-SRC 应工作,因为他们说的,但我猜&LT;脚本&GT; 是有些不同。

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.

推荐答案

不幸的是,你不能以这种方式使用角度。角流程网页后,才页面已经被加载并建造了时间&LT;脚本&GT; 标记已经被处理了一次(脚本标签永远只能运行一次)。如 IMG 其他标记将改变屏幕的外观时,它们的属性加载页面后改变......但如前所述,该脚本只处理的一旦并甚至然后在页面加载和角度才能永远得到控制。

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.

这篇关于如何绑定&LT;脚本&GT;在AngularJS元素的src属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆