我遇到了Iframe指令的問題,我嘗試實現。Angular:Iframe指令中的src屬性錯誤
據我: 模板:
<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由角尚未interpreated) - 一個
http://localhost:8000/app/external.html
(IFRAME SRC一旦被角interpreated)
我想避免無用的第一個電話。如何我可以這樣做嗎?
我試着沒有在模板中的src,並以編程方式將其設置在鏈接或編譯功能,但不會觸發iframe加載。
編輯:jsFiddle添加了錯誤的演示與編譯方法 =>你會螢火/ Chrome瀏覽器開發小組的網絡選項卡中有兩個請求是由看到:
http://fiddle.jshell.net/_display/%7B%7Bsrc%7D%7D
http://fiddle.jshell.net/_display/external.html
感謝您的幫助
當然。你可以這樣做,但它沒有解決問題。 –
你能向我解釋:我錯過了什麼?有什麼不同?如果你想要一個帶有iframe的指令,你仍然可以使用ng-src? – markmarijnissen
使用ng-src工作起來就像一個魅力一樣,從一開始就讓兩個HTTP錯誤消失了。我認爲答案完全符合 –