1

I'm trying to make an Angular directive to wrap an iFrame. Because IE 11 doesn't support the srcdoc attribute I looked for a different way to fill the iFrame:

angular
    .module('angularApp')
    .directive('easyframe', easyframe);

function easyframe() {
    return {
        restrict: 'E',
        scope: {content: '='},
        replace: true,
        template: '<iframe id="easyframe" width="100%"></iframe>',
        controller: function ($sce, $scope, $element) {
            $scope.$watch('content', function () {
                var content = $sce.trustAsHtml($scope.content);
                var iframeDocument = $element[0];

                iframeDocument.open('text/html', 'replace');
                iframeDocument.write(content);
                iframeDocument.close();
             });
        }
    };
}

This fails because the $element I get is a Angular jQueryLite object that doesn't have the open method. How do I get to the DOM element the Angular way, so without using querySelector?

2
  • I got the wrong conclusion, the actual iFrame DOM element is at element[0].contentWindow.document. Can I answer and/or "close" my own question in anyway? Commented Mar 18, 2015 at 13:29
  • just answer your own question - that is fine! Commented Aug 26, 2015 at 10:00

1 Answer 1

3

I ran across a similar problem, but wanted to be able to have the content that is injected into the iframe still be bound to the containing angular scope. I wrote a directive to handle this, which can be found here: http://plnkr.co/edit/KRfAyc5haHyFq7FyCnxg?p=preview

It is used like so:

<wrap-in-frame frame-template="frame-template.html" frame-selector="div" height="200" style="border-width: 5px; width: 100%;">
  Angular Interpolated Data: {{ data }}
  <div>NgModel Data Binding: <input ng-model="data" type="text" /></div>
</wrap-in-frame>

<div>
  Outside frame binding:
  <input ng-model="data" type="text" />
</div>

And produces the following:

Frame Wrapping Example

The frame-template and frame-selector are optional. They allow loading a file (specified by frame-template) into the frame and injecting the content into an element in the frame, specified by frame-selector.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.