【问题标题】:Using angularjs variable inside iframe在 iframe 中使用 angularjs 变量
【发布时间】:2020-02-22 12:51:10
【问题描述】:

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

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

其中 test 刚刚显示为 {{test}} ....

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

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

提前谢谢。

【问题讨论】:

    标签: html angularjs iframe


    【解决方案1】:

    你可以试试这个, 在你的控制器做 - 替换 ng-src="{{test}}" -- 为整个 url 使用一个变量。 然后 -

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

    在你的控制器依赖中也包含 $sce。

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

    【讨论】:

      【解决方案2】:

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

      iframeObject.src = 网址

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

      例子:

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

      因为你还想连接/合并两个字符串,你可以把 JavaScript 改成

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-28
        • 1970-01-01
        • 1970-01-01
        • 2013-12-01
        • 2014-09-05
        相关资源
        最近更新 更多