これは、私のサイトの 1 つで使用した関数です。ディレクティブに入れるコードを修正しました。
注 : iframe では、コンテナに ID が必要ですifrm_container
<iframe resize-iframe id="iframe" src="...">
.directive('resize-iframe', function() {
return {
restrict: 'A',
link: function ( scope, elm, attrs ) {
var container = elm.contentWindow.document.getElementById('ifrm_container'); // Iframe container. Usualy a div.
function autoResize(){
ifrmNewHeight = container.offsetHeight; // New height of the document.
if(ifrmNewHeight !== ifrmOldHeight) {
ifrmOldHeight = ifrmNewHeight + 30; // +30 for no scrollbar.
ifrm.style.height= (ifrmNewHeight + 30) + "px"; // Set iframe style.
}
setTimeout(autoResize, 250);
}
// First call of autoResize().
autoResize();
};
});
IE7 を含むすべてのブラウザーでこのソリューションをテストしましたが、うまく機能しますが、ディレクティブでは機能しません (いくつかの要素が変更されました)。
この指令はテストされていません