【问题标题】:conditionally use either click or href in anchor tag in angular有条件地在角度的锚标记中使用 click 或 href
【发布时间】:2019-12-11 17:48:20
【问题描述】:

您好,我想根据锚标签中的条件使用 href 或 (click)。

我尝试了*ngIf="link.linkName==Grills Data?(click)={{ link.url }}: href={{ link.url }}",但它抛出了错误。

【问题讨论】:

标签: angular typescript angular6


【解决方案1】:

你可以像这样调用函数并将你的条件检查到你的函数中

在component.html中

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

在component.ts中

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

【讨论】:

    猜你喜欢
    • 2019-03-24
    • 2017-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-29
    • 1970-01-01
    • 2012-05-17
    • 2015-12-07
    相关资源
    最近更新 更多