简体   繁体   English

Angular 5:控制台和模板之间的区别

[英]Angular 5: Difference between console and template

I'm trying to hide two columns by the help of the ngIf directive when printing the page. 我正在尝试在打印页面时通过ngIf指令隐藏两列。

When I press the print button, I switch the value of the boolean variable ' print ' to true then I execute the function that convert to PDF. 当我按下打印按钮时,我将布尔变量' print '的值切换为true,然后执行转换为PDF的功能。

  <div class="portlet light">
      <button class="btn btn-danger noprint pull-right" style="margin-bottom: -30px; " (click)="print_table()"> <i class="fa fa-print"></i> Imprimer</button>
      {{print}}
  </div> 

Function that convert to PDF: 转换为PDF的功能:

convertToPdf() {
 const element = document.getElementById('contentToConvert');
 const opt = {
  margin: 0.2,
  filename: 'myfile.pdf',
  image: { type: 'jpeg', quality: 0.98 },
  html2canvas: { scale: 2, width: 1282 },
  jsPDF: { unit: 'mm', format: 'a4', orientation: 'landscape' }
 };

 // New Promise-based usage:
 html2pdf().from(element).set(opt).save();

}

The problem is that even when the value changed in the console, it doesn't in the templete. 问题在于,即使在控制台中更改了值,在模板中也没有更改。

print_table() {
 this.print = true;
 setTimeout(this.convertToPdf, 2000);
 setTimeout(this.toFalse, 5000);
}

toFalse() {
 this.print = false;
 console.log(this.print);
}

Result: 结果:
The value value of 'print' changes just once so the columns stay hidden after the print function knowing that I called toFalse() function to show them. 'print'的值仅更改一次,因此在知道我调用toFalse()函数来显示它们之后,这些列在print函数之后保持隐藏状态。

this in toFalse will be the window object if you pass its reference to setTimeout because setTimeout and setInterval run in the global scope. thistoFalse将是window对象,如果你通过它参考setTimeout因为setTimeoutsetInterval在全球范围内运行。

In your print_table() function, call toFalse() function directly inside the reference of an anonymous function instead of passing a reference of it ie : 在您的print_table()函数中,直接在匿名函数的引用内部调用toFalse()函数,而不是传递它的引用,即:

print_table() {
 this.print = true;
 setTimeout(this.convertToPdf, 2000);
 setTimeout(()=>{this.toFalse()}, 5000);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM