如何在角度6中设置动态下载链接

时间:2019-09-23 10:47:49

标签: angular

大家好,

我想设置动态下载。请帮助我。         这是我的Htmal代码:-

      <button class="btn btn-primary" (click)="downloadMyFile({{account.students[0].schoolId}})"><i class="fa fa-file-pdf-o"></i> Download</button>

这是我的componete.ts方法:-

downloadMyFile( id: number) {
    let filename='Newlands-Year-Book-Draft-V7-Complete-HR.pdf"
    const link = document.createElement('a');
    link.setAttribute('target', '_blank');
    link.setAttribute('href', '/assets/files/'+id+'/'+filename+');
    link.setAttribute('download', '+filename+');
    document.body.appendChild(link);
    link.click();
    link.remove();
  }

我想将id和文件名设置为动态。 但是我无法在点击事件上点击downloadMyFile()函数

2 个答案:

答案 0 :(得分:1)

您应该删除参数周围的插值。插值用于显示元素内部的文本。您不应将其用于参数周围。

  <button class="btn btn-primary" (click)="downloadMyFile(account.students[0].schoolId)">
    <i class="fa fa-file-pdf-o"></i> Download
 </button>

答案 1 :(得分:0)

@Bear和@ Ramesh感谢您的宝贵时间和支持 这是我的答案

<button class="btn btn-primary" (click)="downloadMyFile(account.students[0].schoolId)"><i class="fa fa-file-pdf-o"></i> Download</button>

 downloadMyFile( id: number) {
    let filename = 'Newlands-Year-Book-Draft-V7-Complete-HR.pdf';
    const link = document.createElement('a');
    link.setAttribute('target', '_blank');
    link.download = filename;
    document.body.appendChild(link);
    link.click();
    link.remove();
  }