在Mat-Icon Angular 7中分配名称或ID

时间:2019-05-09 13:38:14

标签: angular-material angular7

我想在mat-icon中获得一些价值,并与“ campaign_id”进行比较

我有两个图标,一个在播放,另一个在暂停,当我单击一个图标时,另一个图标出现

在我的html中,我们有...

<tr *ngFor="let campaign of campaigns">
    <td>{{ campaign.id }}</td>
    <td>{{ campaign.nome_campanha }}</td>
    <td>            
       <label [ngClass]="status[campaign.status].color">{{ status[campaign.status].description }}</label>
    </td>
    <td> 
       <mat-icon id="row-click" matTooltip="Ativar campanha" (click)="occurrence(1, campaign, _icon)" #_icon>play_circle_outline</mat-icon>
       <mat-icon id="row-click" matTooltip="Pausar de campanhas" (click)="occurrence(2, campaign)" *ngIf="run == 1 || run == campaign.id">pause</mat-icon>
    </td>  
</tr>

在TS中,例如ex

occurrence(e, campaign, i) {
  if(user.id == icon.id//icon.name) 
     if(icon.play == show)
       icon.play == hidde
}

这应该是逻辑 点击播放或暂停图标

1 个答案:

答案 0 :(得分:0)

因为您位于*ngFor中,所以所有id="row-click的ID(#_icon<mat-icon>)都相同,所以为了解决这个问题,您可以选择需要使用索引并将其分配给<mat-icon>

例如:

<tr *ngFor="let campaign of campaigns;index=i">
    <td>{{ campaign.id }}</td>
    <td>{{ campaign.nome_campanha }}</td>
    <td>            
       <label [ngClass]="status[campaign.status].color">{{ status[campaign.status].description }}</label>
    </td>
    <td> 
       <mat-icon [id]="'row-click-' + i" matTooltip="Ativar campanha" (click)="occurrence(1, campaign, _icon)" #_icon>play_circle_outline</mat-icon>
       <mat-icon [id]="'row-click-' + i" matTooltip="Pausar de campanhas" (click)="occurrence(2, campaign)" *ngIf="run == 1 || run == campaign.id">pause</mat-icon>
    </td>  
</tr>