javascript - 如何通过 [ngClass] 清除或删除 Angular 6 中的类
问题描述
我有 2 个选项卡,单击一个选项卡 1 将显示一个 div,再次单击切换按钮,该 div 将通过更改类展开(100% 宽度)和折叠(25% 宽度)。当我再次单击 tab2,然后单击 tab1 时,我的 div 应该始终保持折叠状态,我的意思是它的类应该是“旧的”。这是下面的代码。
app.component.html
<span style="cursor:pointer" (click) = "tab1()">Tab1</span> <span (click) = "tab2()" style="cursor:pointer">Tab2</span>
<div [ngClass]="{'old': toggle, 'new': !toggle}" *ngIf="show" class="old">
Hello
</div>
<button (click)="change()">change</button>
app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
toggle:boolean = true;
show:any;
tab1(){
alert('tab1');
this.show = true;
}
tab2(){
alert('tab2');
this.show = true;
}
change(){
this.toggle = !this.toggle;
}
ngOnInit() {
this.show = false;
}
}
app.component.css
.old{
width:25%;
border:1px solid;
height:200px;
cursor:pointer;
background:yellow;
}
.new{
width:100%;
border:1px solid;
height:200px;
cursor:pointer;
background:green;
}
解决方案
尝试这个:
HTML
<div [ngClass]="toggle ? 'old' : 'new'" *ngIf="show">
Hello
</div>
删除了class="old"
. 请立即检查。
推荐阅读
- linux - bash 脚本 - 使用选项而不是选项
- html - 问题后导航栏和侧边栏 css 悬停
- python - 将数据从 S3 传输到 Aurora 时,AWS 粘合作业中的数据截断错误
- user-interface - 是否有针对从 WSL 运行的慢速 X11 服务器的解决方法?
- swift - UIImageView 高度与 UILabel 字体高度相同。在 UIStackView 内居中
- python - NCCLIENT 建立订阅不能处理提取的数据
- azure - Application gateway request_routing_rules does not exist
- typescript - 如何强制这种类型识别子类型约束匹配?
- html - DIv 可见性:隐藏不起作用,控制可见性,而不修改元素的显示 - Bootstrap DIv 标签
- flutter - 1 个集团发出状态列表,而许多集团正在发出自己的状态