如何在mat-list-option(mat-selection-list)项目中添加辅助动作

时间:2018-12-17 12:45:15

标签: angular angular-material2 angular-material-7

如何在mat-list-option元素内的mat-selection-list中添加辅助动作(mat-icon-button)。

当前,所有项目都添加在mat-list-text div中。

示例(https://stackblitz.com/edit/angular-dwac7y):

<mat-selection-list [(ngModel)]="selectedOptions">
    <mat-list-option *ngFor="let item of items;" [value]="item.id" [disableRipple]="true">
        <mat-icon matListAvatar>person</mat-icon>

        <h3 matLine> {{item.name}}</h3>
        <p matLine>
            {{item.details}}
        </p>

        <button mat-icon-button (click)="onEdit($event, item)">
            <mat-icon>edit</mat-icon>
        </button>
    </mat-list-option>
</mat-selection-list>

2 个答案:

答案 0 :(得分:2)

您可以按照以下步骤进行操作。

<h2>Selection-List with secondary action:</h2>

<mat-selection-list [(ngModel)]="selectedOptions">
  <mat-list-option *ngFor="let item of items;" [value]="item.id" [disableRipple]="true">
    <div style="display:flex; justify-content:space-between;align-items:center">
      <div style="display:flex;align-items:center">
        <mat-icon matListAvatar>person</mat-icon>

        <div style="display:flex; flex-direction:column">
          <div>
            <h3 matLine> {{item.name}}</h3>
          </div>
          <div style="margin-top:-30px">
            <p matLine>
              {{item.details}}
            </p>
          </div>
        </div>
      </div>
      <div>
        <button mat-icon-button (click)="onEdit($event, item)">
          <mat-icon>edit</mat-icon>
        </button>
      </div>
    </div>
  </mat-list-option>
</mat-selection-list>

但以下样式...

<div style="margin-top:-30px">

不是一个好的imo解决方案。

enter image description here

答案 1 :(得分:1)

我认为活动应该是这样的。

onEdit(event, item){
    event.stopPropagation();
}