在我看来,您不应该“更改”图标,而是可以根据一些变量/对象状态隐藏/显示图标。这样会是一种更好的方法(例如:因为图标更改是即时的,所以不存在延迟)。我在这里编译了一个 stackblitz 演示,所以你可以查看我的版本:
https://stackblitz.com/edit/angular-63sjtx?file=app%2Flist-sections-example.html
这就是我做 html 的方式,例如:
<mat-list>
<h3 mat-subheader>Folders</h3>
<mat-list-item *ngFor="let folder of folders; let i = index" (click)="toggleArrow(i)" class="list-item">
<mat-icon mat-list-icon [ngClass]="folder.open ? 'hidden' : ''">arrow_right</mat-icon>
<mat-icon mat-list-icon [ngClass]="folder.open ? '' : 'hidden'">arrow_drop_down</mat-icon>
<h4 mat-line>{{folder.name}}</h4>
<p mat-line> {{folder.updated | date}} </p>
</mat-list-item>
</mat-list>
还有打字稿:
import {Component} from '@angular/core';
export interface Section {
name: string;
updated: Date;
open: boolean
}
/**
* @title List with sections
*/
@Component({
selector: 'list-sections-example',
styleUrls: ['list-sections-example.css'],
templateUrl: 'list-sections-example.html',
})
export class ListSectionsExample {
folders: Section[] = [
{
name: 'Photos',
updated: new Date('1/1/16'),
open: false
},
{
name: 'Recipes',
updated: new Date('1/17/16'),
open: false
},
{
name: 'Work',
updated: new Date('1/28/16'),
open: false
}
];
toggleArrow(folderIndex) {
this.folders[folderIndex].open = !this.folders[folderIndex].open;
}
}
最后是 CSS:
.mat-list-icon {
color: rgba(0, 0, 0, 0.54);
}
.list-item {
cursor: pointer;
}
.list-item:hover {
background: #eee;
}
.list-item .hidden{
display: none;
}