有条件地在角标记的锚标记中使用click或href

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

嗨,我想根据锚标签中的条件使用href或(单击)。

我尝试过*ngIf="link.linkName==Grills Data?(click)={{ link.url }}: href={{ link.url }}",但会引发错误。

angular typescript angular6
1个回答
0
投票
您可以像这样调用函数并在函数中检查条件

In component.html

<a (click)="onClick(link.url)"></a>

in component.ts

onClick(link) { if(something) { // do something } else { // the line bellow will navigate to the link window.location.href = link } }

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