angular - How to refresh html element after modifying element -
i have been trying print after modifying html element element has not been changed. (angular2) source code simplified one.
<div *ngif="displaytype === 'screen'"> <div>this screen</div> </div> <div *ngif="displaytype === 'print'"> <div>this print</div> </div>
and when click button following event.
displaytype: string = 'screen'; // default onprint() { this.displaytype = 'print'; let tmp = document.createelement('div'); let el = this.elementref.nativeelement.clonenode(true); tmp.appendchild(el); let content = tmp.innerhtml; let frame1 = document.createelement('iframe'); document.body.appendchild(frame1); let framedoc = frame1.contentwindow; framedoc.document.open(); framedoc.document.write('<html><body>'+content+'</body></html>'); framedoc.document.close(); settimeout(function () { window.frames["frame1"].focus(); window.frames["frame1"].print(); document.body.removechild(frame1); }, 500); }
but contents elements before modifying. how can refresh elements?
to refresh elements need trigger change detection. here post change detection in angular 2: http://blog.thoughtram.io/angular/2016/02/22/angular-2-change-detection-explained.html
i've created plunker here illustrate it: http://plnkr.co/edit/0zwkaq776mkplyzjogyx?p=preview
<button (click)="onprint()">onprint</button>
also should not modify dom elements directly, should create component , use structural directive display (here component has selector "my-print"):
<my-print *ngif="isprint" [iframesrc]="..."></my-print>
Comments
Post a Comment