javascript - iframe inside angular2 component, Property 'contentWindow' does not exist on type 'HTMLElement' -
i have iframe inside angular2 component, , trying change content of iframe accessing contentwindow.
iframe should contain simple button.
my code:
import { component } '@angular/core'; @component({ moduleid: module.id, selector: 'component-iframe', template: '<iframe id="iframe"></iframe>' }) export class componentiframe { constructor() { let iframe = document.getelementbyid('iframe'); let content = '<button id="button" class="button" >my button </button>'; let doc = iframe.contentdocument || iframe.contentwindow; doc.open(); doc.write(content); doc.close(); } }
if comment constructor's code , start app, compiles , runs correctly. uncomment , runs (the button present in iframe).
if decomment code start app (npm start) have compilation bugs message:
property 'contentwindow' not exist on type 'htmlelement'
.
i tried alternative of putting costructor's code ngoninit(), ngaftercontentinit(), ngafterviewinit() error persists.
the template doesn't exist in dom yet when constructor executed
use instead
import { component, viewchild, elementref } '@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(); } }
My eyes were blind
ReplyDelete