2017-12-28 28 views
0

角度で範囲をサブスクライブの下で働いていない私が購読スコープの下this.printInvoice()メソッドを呼び出した場合、それはヌルwindow.open()2

onSubmit() { 
      this.ItemSellService.addItemSale(this.item_sale).subscribe(res => { 
       if (res.success) { 
        this.printInvoice(); 
       } else { 
        // fail message 
       } 
      }) 
     } 

printInvoice(): void { 
       let printContents, popupWin; 
       printContents = document.getElementById('print-section').innerHTML; 
       popupWin = window.open('', '_blank', 'top=0,left=0,height=100%,width=auto'); 
       popupWin.document.open(); 
       popupWin.document.write(` 
       <html> 
        <head> 
        <title>Print tab</title> 
        <style> 
        //........Customized style....... 
        </style> 
        </head> 
       <body onload="window.print();window.close()">${printContents}</body> 
       </html>` 
       ); 
       popupWin.document.close(); 
      } 

しかし、その作業罰金の「文書」のエラーがプロパティを読み取ることができません示してい以下のようなサブスクライブスコープの外形:

onSubmit() { 
    this.printInvoice(); 
        this.ItemSellService.addItemSale(this.item_sale).subscribe(res => { 
         if (res.success) { 
          // I need to call after successful operation 
         } else { 
          // fail message 
         } 
        }) 
       } 

答えて

0

だけで変更されたonloadイベントを=」 window.printInvoice();はwindow.close()」の代わりにonloadイベントの= "window.print();はwindow.close()"

printInvoice(): void { 
       let printContents, popupWin; 
       printContents = document.getElementById('print-section').innerHTML; 
       popupWin = window.open('', '_blank', 'top=0,left=0,height=100%,width=auto'); 
       popupWin.document.open(); 
       popupWin.document.write(` 
       <html> 
        <head> 
        <title>Print tab</title> 
        <style> 
        //........Customized style....... 
        </style> 
        </head> 
       <body onload="window.printInvoice();window.close()">${printContents}</body> 
       </html>` 
       ); 
       popupWin.document.close(); 
      } 
0

はこれを試してみてください:

onSubmit() { 
     var that = this; // use this context where the function works as expected 

     this.ItemSellService.addItemSale(this.item_sale).subscribe(res => { 
      if (res.success) { 
       that.printInvoice(); 
      } else { 
       // fail message 
      } 
     }) 
    } 
+0

それはあなたがコンソールに見てなかった@saif – saif

+0

@dexter動作していないのですか?実行を停止するエラーはありますか? – dexter