如何通过单击文本(而非按钮)来触发功能,href无效

问题描述 投票:0回答:1

我有一行文字(动态)。当用户单击它时,它会触发一个功能。当我不使用标签时,它可以工作。当我添加标签的那一刻(我希望它看起来像一个链接,即使不是),当用户单击文本时,它只会转到主页。这是我尝试过的两种方法:

              <mat-list-item><b class="title"><a href="#" onclick="downloadFile(batchList.dataPath)" class="dlLink" >Data Path:{{batchList.dataPath}}</a></b> 
//this just redirects to the homepage

<mat-list-item (click)="downloadFile(batchList.dataPath)"><b class="title">Data Path:{{batchList.dataPath}} </b>
//this works, as in the function is fired. But it doesn't act like a link, which I want. I've tried using stop propogation, by adding it to the end of my function, like so:

downloadFile(pathToDownload) {
    this.showLoader = true;
    console.log(pathToDownload);
    from(this.downloadFileService.downloadFile({'pathToDownload': pathToDownload}))
      .subscribe((data: any) => {
        saveAs(new Blob([data], {type: 'application/octet-stream'}), (pathToDownload.substring(pathToDownload.indexOf('part'))));
        this.showLoader = false;
        setTimeout(() => {
        }, 300000)
      })
    event.stopPropagation();
     }

//but it doesn't work. 

有什么想法吗?

angular href
1个回答
1
投票

可能是因为您在应使用onClick时使用了(click)。请参考以下示例。

<a href="javascript:void(0);" (click)="downloadFile()" class="dlLink" >Data Path:{{batchList.dataPath}}</a>

Stackblitz example

© www.soinside.com 2019 - 2024. All rights reserved.