angular - 如何动态添加一个类?
问题描述
我已经在 Angular 中编程了很短的时间,但我被困在按钮设计上。我已经用 ngIf 实现了它们,根据它们是否可用动态地出现和消失。问题是我觉得它有点复杂,我想利用相同的逻辑让它们始终显示在屏幕上,但根据我之前使用的相同条件添加颜色。我试图实现 ngClass 的使用,但很明显有些地方是错误的,因为它不起作用。这是我的html:
<div *ngIf="mainFlowchartInstance.eisChartData.value" class="temp_zoom-actions">
<div [ngClass]="{'orangeText' : zoomButtons == 'zoomButtons.reset'}" class="center" (click)="zoomReset()"><i class="icon-refresh"></i></div>
<div [ngClass]="{'orangeText' : zoomButtons == 'zoomButtons.out'}" class="zoom-out" (click)="zoomOut()"><i class="icon-search-minus"></i></div>
<div [ngClass]="{'orangeText' : zoomButtons == 'zoomButtons.in'}" class="zoom-in" (click)="zoomIn()"><i class="icon-search-plus"></i></div>
</div>
这里是我的 ts:
public zoomButtons = {
in: false,
out: true,
reset: false
};
setZoomButtonsState() {
this.zoomButtons.out = !!this.currentZoomPosition;
this.zoomButtons.in = this.zoomButtons.reset = !(this.currentZoomPosition === this.rangeZooms.length - 1);
}
有人让我看到我的错误吗?先感谢您。
解决方案
尝试以这种方式使用它(检查 if zoomButtons.reset
or zoomButtons.out
or zoomButtons.in
are true
or false
,然后应用 class orangeText
):
<div [ngClass]="{'orangeText' : zoomButtons.reset}"
class="center" (click)="zoomReset()">
<i class="icon-refresh"></i>
</div>
<div [ngClass]="{'orangeText' : zoomButtons.out}"
class="zoom-out" (click)="zoomOut()">
<i class="icon-search-minus"></i>
</div>
<div [ngClass]="{'orangeText' : zoomButtons.in}"
class="zoom-in" (click)="zoomIn()">
<i class="icon-search-plus"></i>
</div>
类orangeText
将应用,如果它得到true
之后:
,就像在这个例子中的角度文档:
<some-element [ngClass]="{'first': true, 'second': true, 'third': false}">
在这个地方:
[ngClass]="{'orangeText' : zoomButtons.reset}"
zoomButtons.reset
没有单引号将被视为变量,因此将返回true
or false
。
推荐阅读
- google-people-api - 是否可以通过 Apps 脚本删除 Google 其他联系人?
- disassembly - IDA PRO 更改 .rdata 值
- docker - 入口 Nginx 规则并不总是得到遵守
- apache-spark - 如何将ojai配置从驱动程序传递给spark中的执行程序?
- reactjs - useMemo 钩子不适用于地图元素
- mysql - MYSQL 在一段时间不活动后失去连接
- flutter - 如何使用颤振 mime 包通过幻数获取 mime 类型
- vba - VBA - 如何优化 VBA 代码以更快地运行?
- linux - 牛肉安装“libgcc-9-dev”不可用。我该怎么办?
- reactjs - 如何开一个在 React 中单击按钮