【发布时间】: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%7Dhttp://fiddle.jshell.net/_display/external.html
感谢您的帮助
【问题讨论】:
标签: javascript angularjs iframe angularjs-directive