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?
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:
And produces the following:
The
frame-template
andframe-selector
are optional. They allow loading a file (specified byframe-template
) into the frame and injecting the content into an element in the frame, specified byframe-selector
.