简体   繁体   English

Angular.js:更改加载时的iframe高度并更改内容

[英]Angular.js: Change Iframe height on load & change content

I got an angular app that is dynamically loading a another page into a given iframe. 我有一个角度应用程序,可以将另一个页面动态加载到给定的iframe中。 This iframe's scope is then changed based on input's of the parent page. 然后根据父页面的输入更改此iframe的范围。

I have angular code to load the initial html into the iframe and a piece of plain javascript code to change the iframe size on window change. 我有角度的代码可以将初始html加载到iframe中,也可以使用一段简单的javascript代码在更改窗口时更改iframe大小。

How can I achieve: 我该如何实现:

On initial load of the iframe as well as on each change of the content, I would like to adjust the iframe height to the content (so that basically no scrollbars are visible and content seems to be part of the page). 在iframe的初始加载以及内容的每次更改时,我都希望根据内容调整iframe的高度(这样,基本上没有滚动条可见,内容似乎是页面的一部分)。

My code pieces: 我的代码片段:

HTML with iframe: 具有iframe的HTML:

<div ng-app="cvForm" ng-controller="cvFormController">
    <iframe cvframe="" id="cv-output-frame" style="width: 100%; height: 1000px; border: none"></iframe>
</div>

Loading content into iframe 将内容加载到iframe中

/**
 * Load CV template
 */
cvFormApp.directive('cvframe', function($compile, $http, appConfig) {
    return function($scope, $element, tpl) {
      $http({
        url: appConfig.baseUrl + 'res/templates/mf_classic.html',
        method: 'GET'
      }).then(function(response) {
        var $body = angular.element($element[0].contentDocument.body), 
                    template = response.data;
        $body.append($compile(template)($scope)); //appended compiled element to DOM
      });
    };
});

Code to adjust height on window change: 用于在更改窗口时调整高度的代码:

function resizeIframe() {
    var oIFrame = document.getElementById('cv-output-frame');

    oIFrame.style.height = oIFrame.contentWindow.document.body.scrollHeight + 'px';   
    console.log('cv pane iframe resized to: ' + oIFrame.style.height);
}

document.getElementById('cv-output-frame').onload = resizeIframe; 
window.onresize = resizeIframe;

resizeIframe();

I have played around with directives and watch-statements, but I am still beginner and do not know how to start. 我玩过指令和监视语句,但是我仍然是初学者,不知道如何开始。 My pseudo routine would be something like: 我的伪例程将是这样的:

  1. Load content into iframe 将内容加载到iframe中
  2. Attach watch-directive to iframe 将监视指令附加到iframe
  3. Change on initial load complete the height 初始载荷变化完成高度
  4. On every scope change adjust height to reflect new content (in many cases height will not be affected, as the scope changes are minimal) 在每个范围更改上,调整高度以反映新的内容(在很多情况下,高度不会受到影响,因为范围更改很小)

Thanks for any hints and pointing in the right direction! 感谢您提供任何提示,并指出正确的方向!

Assuming that you have control over the iframe and that both are on the same domain, the best way I know of accomplishing this task is by using the window.postMessage() method. 假设您拥有对iframe的控制权,并且两者都在同一个域中,那么我了解完成此任务的最好方法是使用window.postMessage()方法。

Essentially on any instance where your iframe will have resized, you can call 本质上,在您调整iframe大小的任何情况下,您都可以致电

window.parent.postMessage(yourMessage, 'http://yourdomain.com);

I would probably post back the height to set the containing iframe to, but you could pass anything back. 我可能会回传高度,以将包含iframe设置为该高度,但是您可以将任何内容传递回去。 There are some security concerns, so you should definitely look into those, but they should be easy to get around. 存在一些安全问题,因此您绝对应该研究这些问题,但是它们应该很容易解决。 Then in your resize directive: 然后在您的resize指令中:

angular.module('myApp.directives', [])
    .directive('resize', ['$window', function($window) {
        return {
            restrict: 'A',
            link: function resizeLink(scope, element, attributes) {
                angular.element($window).on('message', function(event) {
                    element.attr('height', event.data + 'px');
                });
            }
        };
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM