简体   繁体   English

Angular-当有人单击嵌套按钮时,如何阻止锚定点击发生

[英]Angular - How do I stop an anchor click from happening when someone clicks on a nested button

I have a div which shows a user notification in my Angular application. 我有一个div,它在Angular应用程序中显示用户通知。

The div is surrounded by an anchor tag which when clicked calls a function that marks the notification as read. div被锚标记包围,单击该锚标记将调用一个将通知标记为已读的函数。

Separately the div contains a button which allows the user to go to the source of the notification. div单独包含一个按钮,该按钮使用户可以转到通知源。

My question is - when the user clicks on the button, how can I stop the anchor that marks the notification as read to be triggered as well? 我的问题是-当用户单击按钮时,如何停止将通知也标记为已读的锚点?

<a (click)="toggleNotificationRead()" >
    <div>
      <button (click)="goTo()" >
          Go To
      </button>  
    </div>
</a>

In my component: 在我的组件中:

goTo() {
  window.location.href = `${this.url}`
};

You need to set the button's type: 您需要设置按钮的类型:

<a (click)="toggleNotificationRead()" >
    <div>
      <button type="button" (click)="goTo()" >
          Go To
      </button>  
    </div>
</a>

additionally, you may need to catch the event and stop propagation, but you shouldn't need to do that: 此外,您可能需要捕获事件并停止传播,但是您不必这样做:

<a (click)="toggleNotificationRead()" >
    <div>
      <button type="button" (click)="goTo($event)" >
          Go To
      </button>  
    </div>
</a>

goTo(event: Event) {
  event.stopPropagation();
  window.location.href = `${this.url}`
};

as a side note, this HTML isn't valid. 附带说明,此HTML无效。 You have block-level elements inside of inline elements. 内联元素中有块级元素。 I would seriously consider restructuring this code. 我将认真考虑重组此代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用户单击按钮时如何在angular4中单击时显示消息 - How to show a message on click in angular4 when user clicks on a button 使用Ionic 2在iOS上单击“后退”按钮时,如何阻止导航栏消失? - How do I stop the navbar from disappearing when I click the back button on iOS using Ionic 2? 当我单击按钮执行功能时,Angular 8 HTTP POST 什么也没有发生 - Angular 8 HTTP POST nothing happening when I click the button to execute function Angular 2中的queryParams正在编码,如何防止这种情况发生? - queryParams in Angular 2 being encoded, how do I prevent this from happening? 我正在使用 angular 7,当我单击锚点按钮时,routerLink 不起作用 - i am using angular 7 when routerLink not working when i click on anchor button it's not working 用户单击提交按钮时如何捕获日期和时间? 角度2 - How do you capture the date and time when a user clicks the submit button? Angular 2 单击关闭按钮时如何停止激活 angular 反应式表单验证 - How can I stop the angular reactive form validation from activating when clicking close button 如何使用jquery datatable单击角度4中的按钮或锚点标签来导航页面 - how to navigate a page on click a button or anchor tag in angular 4 with jquery datatable 如何通过角度按钮单击在MS Excel中打开新下载的Excel文件? - How do I open a newly downloaded excel file in MS Excel from a button click in angular? 如何在单击按钮时将 id 和对象从一个组件传递到另一个组件的 angular 函数? - How do I pass an id and object on click of a button from one component to another component' s function in angular?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM