如何通过单击文本(而不是按钮)来触发功能 href 不起作用
How to fire function by clicking on text(not button) a href doesn't work
我有一行文字(动态)。当用户点击它时,它会触发一个功能。当我不使用 a 标签时它会起作用。在我添加 a 标签的那一刻(我希望它看起来像 link,即使它不是),当用户单击文本时,它只会转到主页。以下是我尝试过的两种方法:
<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.
有什么想法吗?
这可能是因为您在应该使用 (click)
的时候使用了 onClick
。请参考下面的例子。
<a href="javascript:void(0);" (click)="downloadFile()" class="dlLink" >Data Path:{{batchList.dataPath}}</a>
我有一行文字(动态)。当用户点击它时,它会触发一个功能。当我不使用 a 标签时它会起作用。在我添加 a 标签的那一刻(我希望它看起来像 link,即使它不是),当用户单击文本时,它只会转到主页。以下是我尝试过的两种方法:
<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.
有什么想法吗?
这可能是因为您在应该使用 (click)
的时候使用了 onClick
。请参考下面的例子。
<a href="javascript:void(0);" (click)="downloadFile()" class="dlLink" >Data Path:{{batchList.dataPath}}</a>