如何在角4中更改弹出窗口的字体?

时间:2018-02-27 18:52:54

标签: html angular popupwindow

我使用角度,我有一个弹出窗口来打印页面。这是我的component.ts中的代码:

 headContents = document.getElementById('head-section').innerHTML;
innerContents = document.getElementById('inner-section').innerHTML;
signContents = document.getElementById('sign-section').innerHTML;
popupWin = window.open('', '_blank', 'top=0,left=0,height=auto,width=auto');
// console.log(popupWin)
popupWin.document.open();
popupWin.document.write(
  '<html><head><style> body{width: ' + this.width + '; height: ' + this.height + ';}' +
  '.head{padding-left: ' + this.date_padding_left + '; padding-right: ' + this.date_padding_right + '; padding-top: ' + this.date_padding_top + ' ; padding-bottom: ' + this.date_padding_bottom + '; direction: rtl} ' +
  '.inner{padding-left: ' + this.text_padding_left + '; padding-right: ' + this.text_padding_right + '; padding-top: ' + this.text_padding_top + '; padding-bottom: ' + this.text_padding_bottom + '; text-align: justify; direction: ' + this.rtl + ';white-space: pre-wrap; word-wrap: break-word;width: 166mm;max-height: 873px} ' +
  '.sign{padding-left: ' + this.sign_padding_left + '; padding-right: ' + this.sign_padding_right + '}</style>' +
  '</head><body onload="window.print();window.close()"><p class="head">' + headContents + '</p> <p class="inner">' + innerContents + '</p> <p class="sign">' + signContents + '</p></body></html>');
popupWin.document.close();

现在我想更改.inner类字体。我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

好的,我刚试过这个,似乎工作正常。所以,现在你的ts文件应该是这样的(确保将你的字体文件放在src / assets文件夹中):

import { Component } from '@angular/core';

@Component({
   selector: 'app-root',
   templateUrl: './app.component.html',
   styleUrls: ['./app.component.css']
})
export class AppComponent {
   title = 'app';
   headContents = "Header";
   innerContents = "Contents pending...";
   signContents = "Signs, signs, Everywhere signs!";

   showWin()
   {
      let popupWin = window.open('', '_blank', 'top=0,left=0,height=auto,width=auto');

      popupWin.document.open();
      popupWin.document.write('<html><head><style>'+
      '@font-face {font-family:myFont;src:url("assets/Pacifico.ttf");}'+
      '.inner{font-family: myFont;font-size: 40pt;text-align: justify;}'+
      '</style></head><body onload="window.print();window.close()"><p class="head">' + this.headContents + '</p> <p class="inner">' + this.innerContents + '</p> <p class="sign">' + this.signContents + '</p></body></html>');
      popupWin.document.close();
   }
}

你的html文件应该是这样的:

<button (click)="showWin()">Show Window</button>

另外,您可能希望使用绑定来从HTML文件中获取值,而不是使用 document.getElementById()