使用 <script src="..."> 加载外部脚本;在角 [英] Loading an external script using <script src="..."> in Angular

查看:40
本文介绍了使用 <script src="..."> 加载外部脚本;在角的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 URL 中包含一个具有会话 ID 的脚本(我通常不会这样做,但我使用 Crocodoc 将文档嵌入到页面中,他们似乎没有其他方法它不使用不可自定义的 iFrame):

I need to include a script that has a session ID in the URL (I wouldn't normally do this, but I'm using Crocodoc to embed documents into the page and they don't seem to have another way of doing it without using an iFrame which isn't customizable):

<!--sets a global variable "_doc" that is needed for initialization-->
  <script src="//crocodoc.com/webservice/document.js?session=tohY5vh3dPjUbmW6_imSQFshvQUsJ3fuJFyG7CxBU-E3AArTbELI3U0bSJBm6z5ZKtXpJQCnJ-EU1J2WGbuu6WH4e3Bglcy38TplHg"></script>

我尝试了一些简单的事情,例如:

I tried simple things like:

<script ng-src="//crocodoc.com/webservice/document.js?session={{ file.crocodoc_session }}"></script>

这可以很好地写出标记,但从未请求过脚本.我还尝试仅使用src"而不是ng-src",但这会在 URL 中使用{{ file.crocodoc_session }}"发出请求,并且在实际写入会话 ID 值后不会发出另一个请求.

This writes the tag out fine, but the script is never requested. I also tried using just "src" instead of "ng-src" but that makes a request with "{{ file.crocodoc_session }}" in the URL, and doesn't make another request once the session ID value is actually written.

我尝试的最后一件事是使用编译函数创建指令,在该指令中我使用 jQuery 设置 src 属性.那行得通,但我不知道如何将会话 ID 传递给编译函数.我在元素上使用了会话"属性,但是当我传递一个变量(或使用 session="{{ file.crocodoc_session }}")时,我将变量名作为字符串而不是它的值.

The last thing I tried was to make a directive with a compile function, in which I use jQuery to set the src attribute. That worked, expect that I couldn't figure out how to pass the session ID to the compile function. I used a "session" attribute on the element, but when I pass a variable (or use session="{{ file.crocodoc_session }}") I get the variable name as a string instead of it's value.

推荐答案

尝试评估控制器内的链接,将其分配给作用域变量,然后在 ng-src 中引用它.

Try evaluating the link inside the controller, assigning it to a scope variable and then referencing it in the ng-src.

这篇关于使用 &lt;script src="..."&gt; 加载外部脚本;在角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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