Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 Change Class On Condition

I have three buttons in a view and on page load I am displaying the first buttons content. Once a button is clicked the button becomes active but on page load the initial button is not set to active. In order to show the first button active I added this to the div:

[ngClass]="'active'" 

The problem with this is now when another button is clicked the first button keeps the active class. I am evaluating the data being show based on a string. On click of each button the string changes.

How can I add a condition to check for the current string? So if the string matches the current data being show then add the active class to this button?

So something like this is what I am looking for:

[ngClass]="'active'" if "myString == ThisIsActiveString;

This is my current button, but the class is not added when I add it in this syntax:

 <button  [class.active]="shown == EQUIFAX" (click)="shown = 'EQUIFAX'" type="button" id="equifax" class="btn btn-secondary">Equifax Report </button>

To be clear the string is defaulted to "EQUIFAX" on page load.

This is based on answer:

 <button [ngClass]="'active' : shown == 'EQUIFAX'" (click)="shown = 'EQUIFAX'" type="button" id="equifax" class="btn btn-secondary">Equifax Report </button>
like image 226
wuno Avatar asked Nov 08 '16 18:11

wuno


People also ask

How do you give ngClass a condition?

To add a conditional class in Angular we can pass an object to ngClass where key is the class name and value is condition i.e., true or false as shown below. And in the above code, class name will be added only when the condition is true.

Can I use both class and ngClass?

You can use both class and ngClass as the first one gives you the opportunity to apply a class that you want to implement in all cases under any circumstances and the later to apply classes conditionally.

What is the difference between class and ngClass?

With class binding, we can add only single class conditionally. However to set multiple classes we can use ngClass directive.

What does ngClass do in Angular?

ngClass is a directive in Angular that adds and removes CSS classes on an HTML element.


4 Answers

You can add css class based on condition in NgClass directive itself:

[ngClass]="{ 'active': myString == ThisIsActiveString }";

You can read more about NgClass directive and find examples here.

like image 140
Stefan Svrkota Avatar answered Oct 16 '22 04:10

Stefan Svrkota


Your equivalence statement of [class.active]="shown == EQUIFAX" is comparing shown to a variable named EQUIFAX.

You should, instead, change the equivalency to compare to the string [class.active]="shown == 'EQUIFAX'"

Using [ngClass] would get you [ngClass]="{'active': shown == 'EQUIFAX'}

Here's a playground with this implemented: http://plnkr.co/edit/j2w8aiQPghl0bPZznoF2?p=preview

like image 22
silentsod Avatar answered Oct 16 '22 05:10

silentsod


If you want for example to add many classes and have two or more conditions, each class and condition you can add after , comma.

For example

[ngClass]="{'first-class': condition,
            'second-class': !condition}"
like image 3
Druta Ruslan Avatar answered Oct 16 '22 03:10

Druta Ruslan


you can do that:

[class.nameForYourClss]="myString == ThisIsActiveString ";
like image 2
yanai edri Avatar answered Oct 16 '22 03:10

yanai edri