javascript - 如何将 ngClass 仅应用于一个表头

标签 javascript html css angular

HTML

<th>Car Name <i (click)="sortData('carName')" [ngClass]="custom" class="fa sort-icon"></i></th>

<th>Car Status <i (click)="sortData('carStatus')" [ngClass]="custom" class="fa sort-icon"></i></th>

TS

sortData(name) {
  this.pipeFlag = !this.pipeFlag;
  this.custom = (this.pipeFlag === true) ? 'fa-arrow-down' : 'fa-arrow-up';
}

我正在尝试在单击时将 Font Awesome 类应用于表头,但它已应用于两个表头。如何仅将它应用到我单击的表头?

最佳答案

您将两个表头的 ngClass 绑定(bind)到同一个变量:[ngClass]="custom",因此当 custom 更改时,两个表头都将更新。

您可以创建一个对象来保存不同元素的类,如下所示:

public custom = {
  carName: 'fa-arrow-down'
  carStatus: 'fa-arrow-down'
};

那么您的sortData 方法将如下所示:

sortData(name) {
  this.pipeFlag = !this.pipeFlag;
  this.custom[name] = (this.pipeFlag === true) ? 'fa-arrow-down' : 'fa-arrow-up';
}

和 HTML:

<th>Car Name <i (click)="sortData('carName')" [ngClass]="custom['carName']" class="fa sort-icon"></i></th>
<th>Car Status <i (click)="sortData('carStatus')" [ngClass]="custom['carStatus']" class="fa sort-icon"></i></th>

关于javascript - 如何将 ngClass 仅应用于一个表头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61755578/

相关文章:

javascript - 是 tabIndex : 0 in links considered a good practice in order to avoid href ="javascript: void(0);"?

javascript - Azure 网站单点登录以用户身份从 Azure Active Directory 访问 Azure 移动服务

javascript - 隐藏和显示切换/更改按钮颜色,不适用于功能

javascript - 部分透明(opacity)的HTML5 Canvas绘图

jquery - 获取固定宽度的可滚动区域

css - 如何比较两种颜色?

javascript - 将图像和文本设置为默认

javascript - 如何将removePostDataItem与新的jqgrid一起使用

html - 如何让我的 div 中的图像在悬停时改变不透明度?

html - 为什么开发工具提供 Quirks 模式?