【发布时间】:2021-07-26 01:06:33
【问题描述】:
有没有什么方法可以为 Javascript 中的对象设置 default 键,就像我们可以使用 switch 一样?
如果任何其他键不匹配,我想调用该特定键。场景请参考以下示例:
app.component.css
.custom {
color: aqua;
}
.success {
color: green;
}
.danger {
color: red;
}
.wetght {
font-weight: bold;
}
app.component.html
<input class="wetght default {{custom[xyz.abc]}}" [value]="xyz.abc"/><br>
<input class="witght {{custom[xyz.def]}}" [value]="xyz.def"/><br>
<button (click)="abc()">abc</button>
app.component.ts
export class AppComponent {
xyz = {abc: '', def: ''};
custom = {
'PASS': 'success',
'FAIL': 'danger'
};
abc() {
this.xyz = {abc: 'PASS', def: 'uuu'};
}
}
所以这是我的情景。我想要的是,如果名为custom 的对象变量中不存在密钥,那么它应该寻找默认密钥?
我已经尝试过使用ng-class,它运行良好,但我想深入了解一些不同的东西。
我也为上述场景创建了一个有效的example。
【问题讨论】:
标签: javascript html css angular typescript