javascript - Angular how to conditionally change css class? - Stack Overflow

I have an icon with a default class , the class name is card-icon , now how do I change the class based

I have an icon with a default class , the class name is card-icon , now how do I change the class based on condition or disable the class ?

Because the default color of the icon is gray now when condition is met or condition true I want to change it to black color.

The condition is below , when condition is true disable class="card-icon" or if it is not possible to disable then replace when a new class for example [class.newclass] .

Any idea how to implement this guys ? Thank you.

#Icon with default class

 <mat-icon class="card-icon">group</mat-icon>

#Condition

<mat-icon class="active-icon" [class.newclass]="currentTabElement === 'group'">group</mat-icon>

I have an icon with a default class , the class name is card-icon , now how do I change the class based on condition or disable the class ?

Because the default color of the icon is gray now when condition is met or condition true I want to change it to black color.

The condition is below , when condition is true disable class="card-icon" or if it is not possible to disable then replace when a new class for example [class.newclass] .

Any idea how to implement this guys ? Thank you.

#Icon with default class

 <mat-icon class="card-icon">group</mat-icon>

#Condition

<mat-icon class="active-icon" [class.newclass]="currentTabElement === 'group'">group</mat-icon>
Share Improve this question asked Jul 25, 2021 at 15:25 user16477472user16477472 4
  • 1 Isn't this a duplicate of stackoverflow./questions/35269179/… – Janos Vinceller Commented Jul 25, 2021 at 15:39
  • I think No Sir ,. – user16477472 Commented Jul 25, 2021 at 15:43
  • I mean it's basically the same thing. With that information in mind you can build your own solution. And wele to StackOverflow, anyway! – Janos Vinceller Commented Jul 25, 2021 at 15:54
  • Does this answer your question? Angular: conditional class with *ngClass – R. Richards Commented Jul 25, 2021 at 16:46
Add a ment  | 

2 Answers 2

Reset to default 5

you can use ng class property in angular to switch between CSS classes as an example, if I want to create a toggle button that needs to switch between the dark background and light background I can do the following changes.

HTML file
<div [ngClass]="{'black-background':blacked, 'white-background':!blacked}">

CSS / SCSS file

.black-background {
    background-color: #000;
}
.white-background {
    background-color: #FFF;
}

TypeScript file In the typescript file, you need to have the property blacked value which we pass in the HTML tag need to be true or false

blacked = true;

for now, the blacked value is true, so black-background value is true so now we can see the black background. If property blacked=false; then the white-background value is true because we pass white-background:!blacked. So like that we can toggle between the CSS classes.

Use ngClass.

 <mat-icon [ngClass]="{'card-icon': true, 'active-icon': currentTabElement === 'group'}">group</mat-icon>

The code above will add the card-icon class to all icons, and the active-icon class only when the condition currentTabElement === 'group' evaluates to true.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745334351a4623033.html

相关推荐

  • javascript - Angular how to conditionally change css class? - Stack Overflow

    I have an icon with a default class , the class name is card-icon , now how do I change the class based

    11小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信