【问题标题】:Angular : src attribute bug in Iframe directiveAngular:Iframe 指令中的 src 属性错误
【发布时间】:2013-08-21 10:10:18
【问题描述】:

我尝试实施的 Iframe 指令有问题。

就我而言: 模板:

<div class="externalIframe" iframe-src="external.html"></div>

指令:

angular.module('project.directives', [])
   .directive('externalIframe', ['$rootScope', function($rootScope) {
      return {
        restrict: 'C',
        replace: true,
        transclude: true,
        scope: {
          src: '@iframeSrc', // the src uses the data-binding from the parent scope
        },
        template: '<iframe src="{{src}}" height="100%" width="100%" frameborder="0"></iframe>',
        link: function(scope, elem, attrs) {
          //elem.src = "dummy.html"; // not working either
        }
      }
    }])

问题:它触发 2 个 HTTP 请求(2 个 iframe 加载)。 :

  • 一到http://localhost:8000/app/{{src}}(iframe src 尚未被 angular 解释)
  • 一到http://localhost:8000/app/external.html(iframe src 曾经被 angular 解释)

我想避免无用的第一次通话。我该怎么做?

我尝试在模板中不使用 src,并以编程方式将其设置在链接或编译函数中,但这不会触发 iframe 加载。

编辑:jsFiddle 添加了带有编译方法的错误演示 => 您会在 firebug/chrome 开发面板的网络选项卡中看到发出了两个请求:

  • http://fiddle.jshell.net/_display/%7B%7Bsrc%7D%7D
  • http://fiddle.jshell.net/_display/external.html

感谢您的帮助

【问题讨论】:

    标签: javascript angularjs iframe angularjs-directive


    【解决方案1】:

    您不需要为此指定指令。在实际的 iframe 元素上使用 ng-src。请参阅docs on ng-src

    <iframe ng-src="external.html"></iframe>
    

    【讨论】:

    • 当然。你可以这样做。但它并没有解决提到的问题。
    • 你能向我解释一下:我错过了什么吗?与上述问题有什么区别?如果你想要一个带有 iframe 的指令,你仍然可以使用 ng-src?
    • 使用 ng-src 就像一个魅力,让两个 HTTP 错误从一开始就消失了。我认为答案完全符合问题。
    • 小心,这是当前 ng-src 实现中的一个错误:github.com/angular/angular.js/issues/9843
    【解决方案2】:

    从模板中的 iframe 中删除 src 并简单地更改链接函数中的属性(通过 element.attr())即可:

    return {
        restrict: 'E',
        require: '?ngModel',
        replace: true,
        transclude: true,
        template: '<iframe height="100%" width="100%" frameborder="0"></iframe>',
        link: function (scope, element, attrs) {
            element.attr('src', attrs.iframeSrc);
        }
    };
    

    小提琴: http://jsfiddle.net/5rYrw/

    【讨论】:

    • 谢谢,element.attr( 部分很有用。
    【解决方案3】:

    不要使用“链接”,而是使用“编译”功能,因为您本质上是想在插入 DOM 之前修改 HTML。我认为“链接”被插入,然后绑定到范围。

    所以有链接 1. 使用 {{url}} 调用编译 - 发出 iframe 请求 2.链接被调用,{{url}}被替换,因此你第二次调用。

    如果你使用'compile',你可以自己修改src属性。

    http://docs.angularjs.org/guide/directive看看,希望对你有帮助

    编辑 检查这个小提琴http://jsfiddle.net/jbSx6/20/

    return {
        restrict: 'E',
        require: '?ngModel',
        replace: true,
        transclude: true,
        template: '<iframe src="%url%" height="100%" width="100%" frameborder="0"></iframe>',
        compile: function (element, attrs, transclude) {
            console.log(element[0].outerHTML);
            element[0].outerHTML = element[0].outerHTML.replace('%url%',attrs.iframeSrc);
            console.log(element);
        }
    };
    
    <div ng-app="myApp">
       <div>display google in frame</div>
       <my-frame data-iframe-src="http://jsfiddle.net">test</my-frame>
    </div>
    

    【讨论】:

    • 您好,感谢您的帮助,但我尝试了但没有成功。有关详细信息,请参阅我的编辑/jsfiddle。
    • 嗨 Bixi - 它确实有效,在那个小提琴中你仍在使用链接功能,而不是编译。请查看我的编辑。
    • 这绝对是解决问题的“Angular 方式”。很好的答案。
    猜你喜欢
    • 2016-11-19
    • 1970-01-01
    • 2016-06-03
    • 2019-11-23
    • 1970-01-01
    • 2021-02-11
    • 2015-06-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多