繁体   English   中英

在 iframe 中使用 angularjs 变量

[英]Using angularjs variable inside iframe

所以我想在 iframe src 或 ng-src 属性中使用一个变量。 显然,无论我使用什么,我的变量都没有被识别出来。 例如`

<iframe ng-src="http://www.example.com/?name={{test}}">
</iframe>

其中 test 只是显示为{{test}} ....

此外,当我使用$sce将其用作受信任的 url 时,它不起作用,普通 src 也是如此..

有什么我看不到的吗? 有关于 Iframes 和 angularjs 的东西吗? 请分享,它真的很烦人。

提前谢谢。

你可以试试这个,在你的控制器中 - 替换ng-src="{{test}}" - 对整个 url 使用一个变量。 进而 -

$scope.test=$sce.trustAsResourceUrl("https://www.example.com");

还将 $sce 包含在您的控制器依赖项中。

它对我有用..希望它有帮助。

您可以使用以下语法设置 src 属性:

iframeObject.src = URL

注意URL指定要嵌入的内容的 URL,它可以是绝对 URL 或相对 URL。

例子:

 document.getElementById('embedContent').src = "http://www.example.com";
 #embedContent{ width: 600px; height: 500px; }
 <iframe id="embedContent" src="#"></iframe>

因为您还想连接/合并两个字符串,您只需将 JavaScript 更改为

document.getElementById('embedContent').src = "http://www.example.com/?name=" + test;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM