Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
97 views
in Technique[技术] by (71.8m points)

html - Is there a way of adding two buttons inside of a button?

I am creating a dropdown component for a notifications tray.

<ce-dropdown #notificationsDropdown
           className="dropdown-menu dropdown-icon">
<button mat-button
        *ngFor="let notification of notifications"
        (click)="markAsRead(notification)"
        class="animated-transition-2">
  <strong *ngIf="notification.read === null">
    {{notification.message | translate: getParamsData(notification)}}
  </strong>
  <span *ngIf="notification.read !== null">
    {{notification.message | translate: getParamsData(notification)}}
  </span>
  <mat-icon class="mdi mdi-delete-forever"
            *ngIf="!isEmptyTray()"
            (click)="deleteNotification(notification)"></mat-icon>
</button>

It should look like this:

Image of the notification to be shown

As you can see, I have a list of notifications, and for each one of them I create a button with a text that if the notification is read is bold, and I also have a delete icon. When I click on the message I'm supposed to mark the notification as read, and if I click on the icon I should delete it.

The icon was the last addition, but now I realize that when I click on the icon, both actions take place. I want to separate it, but so far I realize that the button is the only one that allows onClick events, right? Cause the other approaches are not working for me.

I've tried this:

  <ce-dropdown #notificationsDropdown
           className="dropdown-menu dropdown-icon">
<div *ngFor="let notification of notifications">
  <button mat-button
          (click)="markAsRead(notification)"
          class="animated-transition-2">
    <strong *ngIf="notification.read === null">
      {{notification.message | translate: getParamsData(notification)}}
    </strong>
    <span *ngIf="notification.read !== null">
      {{notification.message | translate: getParamsData(notification)}}
    </span>
  </button>
  <button mat-button
          (click)="deleteNotification(notification)"
          class="animated-transition-2">
    <mat-icon class="mdi mdi-delete-forever"
              *ngIf="!isEmptyTray()"
              ></mat-icon>
  </button>
</div>

But that is not working for me. It looks like this, even with the display: flex option:

Image of the list of divs

I also tried this:

<ce-dropdown #notificationsDropdown
           className="dropdown-menu dropdown-icon">
<button mat-button
        *ngFor="let notification of notifications"
        class="animated-transition-2">
  <div (click)="markAsRead(notification)">
  <strong *ngIf="notification.read === null">
    {{notification.message | translate: getParamsData(notification)}}
  </strong>
  <span *ngIf="notification.read !== null">
    {{notification.message | translate: getParamsData(notification)}}
  </span>
  </div>
  <mat-icon class="mdi mdi-delete-forever"
            *ngIf="!isEmptyTray()"
            (click)="deleteNotification(notification)"></mat-icon>
</button>

Please does anyone has any idea how can I do it, and keep the for cycle, which is the main reason I want to do this inside of the cycle.

question from:https://stackoverflow.com/questions/65849742/is-there-a-way-of-adding-two-buttons-inside-of-a-button

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Please try doing this:

(click)="deleteNotification(notification); $event.stopPropagation()"

It is similar to @Thibs's proposal but in the opposite order because the propagation is from the nested button to the outer one. Therefore you should stop it after you process it locally (your function).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...