iframe inside angular2 component, Property 'contentWindow' does not exist on type 'HTMLElement'
Solution 1:
The template doesn't exist in the DOM yet when the constructor is executed
Use instead
import { Component, ViewChild, ElementRef } from '@angular/core';
@Component({
moduleId: module.id,
selector: 'component-iframe',
template: '<iframe #iframe></iframe>'
})
export class ComponentIframe {
@ViewChild('iframe') iframe: ElementRef;
ngAfterViewInit() {
let content = '<button id="button" class="button" >My button </button>';
let doc = this.iframe.nativeElement.contentDocument || this.iframe.nativeElement.contentWindow;
doc.open();
doc.write(content);
doc.close();
}
}
Solution 2:
use this:
let iframe = document.getElementById('iframe') as HTMLIFrameElement