javascript Angular:Iframe 指令中的 src 属性错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18354636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angular : src attribute bug in Iframe directive
提问by Cétia
I'm having a problem with a Iframe directive that I try to implement.
我尝试实现的 Iframe 指令有问题。
As far I am : Template:
就我而言:模板:
<div class="externalIframe" iframe-src="external.html"></div>
Directive :
指令:
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
}
}
}])
Problem : It fires 2 HTTP request (2 iframe loading). :
问题:它触发 2 个 HTTP 请求(2 个 iframe 加载)。:
- one to
http://localhost:8000/app/{{src}}
(iframe src not yet interpreated by angular) - one to
http://localhost:8000/app/external.html
(iframe src once interpreated by angular)
- 一到
http://localhost:8000/app/{{src}}
(iframe src 尚未被角度解释) - 一到
http://localhost:8000/app/external.html
(iframe src 曾经由 angular 解释)
I want to avoid the useless first call.. How can I do that ?
我想避免无用的第一次调用.. 我该怎么做?
I tried without src in the template and programmatically set it in the link or compile function but that doesn't fire the iframe loading.
我尝试在模板中不使用 src 并以编程方式在链接或编译函数中设置它,但这不会触发 iframe 加载。
edit: jsFiddleadded for bug demo with compile method=> you'll see in the network tab of firebug/chrome dev panel that two request are made :
编辑:jsFiddle为使用 compile 方法的 bug 演示添加=> 您将在 firebug/chrome dev 面板的网络选项卡中看到发出了两个请求:
http://fiddle.jshell.net/_display/%7B%7Bsrc%7D%7D
http://fiddle.jshell.net/_display/external.html
http://fiddle.jshell.net/_display/%7B%7Bsrc%7D%7D
http://fiddle.jshell.net/_display/external.html
Thanks for the help
谢谢您的帮助
回答by markmarijnissen
You do not need a directive for this. Use ng-src
on an actual iframe
element. See the docs on ng-src.
您不需要为此制定指令。使用ng-src
一个实际的iframe
元素。请参阅ng-src 上的文档。
<iframe ng-src="external.html"></iframe>
回答by tdakhla
Removing src from the iframe in the template and simply changing the attribute in the link function (via element.attr()) works:
从模板中的 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);
}
};
Fiddle: http://jsfiddle.net/5rYrw/
回答by leon
Instead of using 'link' use the 'compile' function, as you're essentially wanting to modify the HTML before insertion into the DOM. I think 'link' is inserted, and then bound to the scope.
不要使用“链接”,而是使用“编译”函数,因为您本质上是想在插入 DOM 之前修改 HTML。我认为插入了“链接”,然后绑定到范围。
So with link 1. compile is called with {{url}} - request by iframe is made 2. link is called, and {{url}} is replaced, hence you second call.
因此,对于链接 1. compile 用 {{url}} 调用 - iframe 发出请求 2. 调用链接,并替换 {{url}},因此您第二次调用。
If you use 'compile' you can modify the src attribute yourself.
如果您使用“编译”,您可以自己修改 src 属性。
Give http://docs.angularjs.org/guide/directivea look over, hope this helps
给http://docs.angularjs.org/guide/directive看看,希望这有帮助
EditCheck this fiddle http://jsfiddle.net/jbSx6/20/
编辑检查这个小提琴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>