Reputation: 93
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?
Upvotes: 1
Views: 1346
Reputation: 9142
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:
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
.
Upvotes: 3