简体   繁体   English

如何在角度中切换Mat-icon?

[英]How to toggle mat-icon in angular?

i have multiple mat-icon (which comes dynamically) in mat-table. 我在垫子桌子上有多个垫子图标(动态出现)。 when i click on specific mat-icon for toggle mat-icon, it toggles all mat-icons but i want to toggle only clicked mat-icon. 当我单击特定的Mat-icon切换Mat-icon时,它切换了所有Mat-icon,但是我只想切换单击的Mat-icon。 how to do this? 这个怎么做?

follow.component.html follow.component.html

<table mat-table [dataSource]="dataSource">
  <ng-container matColumnDef="username">
    <th mat-header-cell *matHeaderCellDef> Full Name </th>
    <td mat-cell *matCellDef="let element"> {{element.username}} </td>
  </ng-container>

 <ng-container matColumnDef="action">
   <th mat-header-cell *matHeaderCellDef> Follow </th>
     <td mat-cell *matCellDef="let element">
       <button mat-mini-fab color="primary" (click)="toggleIcon()"><mat-icon>{{icon}}</mat-icon></button>
    </td>
 </ng-container>
</mat-table>

follow.component.ts follow.component.ts

  dataSource : MatTableDataSource<PeriodicElement> ;
  displayedColumns: string[] = ['username','action'];  

toggleIcon() {
  if (this.icon === 'person_add_disabled') {
    this.icon = 'person_add';
  } else {
    this.icon = 'person_add_disabled'
  }
}

this.supportService.getUsersListForFollowing({'userid':this.userid}).
  subscribe((data) => {
   if(data.status == 1){
     this.dataSource = new MatTableDataSource<PeriodicElement>(data.payload);
   }
  }
);

export interface PeriodicElement {
  username : string;
}

I'm not familiar with the angular material but I think you should include the disabled-information in the element itself. 我对角度材料不熟悉,但是我认为您应该在元素本身中包含禁用信息。 You can easily output the desired icon in the icon component with a ternary operator. 您可以使用三元运算符在图标组件中轻松输出所需的图标。

<td mat-cell *matCellDef="let element">
   <button mat-mini-fab color="primary" (click)="element.disabled = !element.disabled"><mat-icon>{{element.disabled ? 'person_add_disabled' : 'person_add'}}</mat-icon></button>
</td>

The following statement toggles the disabled property of your row 以下语句切换行的Disabled属性

(click)="element.disabled = !element.disabled"

This ternary operator returns the desired string used by mat-icon 该三元运算符返回mat-icon使用的所需字符串

<mat-icon>{{element.disabled ? 'person_add_disabled' : 'person_add'}}</mat-icon>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM