45

Is there a way to add a class from the .ts file, using Angular solutions

<div [class.extra-sparkle]="isDelightful == true">

I want to do the above but from the side of the .ts file. The less code the better.

<button class="general" (click)="ChangeScreen('Global')"       [class.selected]="CurrentPage == 'Global'">Global</button>
<button class="general" (click)="ChangeScreen('Maintenance')"  [class.selected]="CurrentPage == 'Maintenance'">Maintenance</button>
<button class="general" (click)="ChangeScreen('Settings')"     [class.selected]="CurrentPage == 'Settings'">Settings</button>
<button class="general" (click)="ChangeScreen('Profile')"      [class.selected]="CurrentPage == 'Profile'">Profile</button>
<button class="general" (click)="ChangeScreen('Transactions')" [class.selected]="CurrentPage == 'Transactions'">Transactions</button>

I would like to just add something like this into the ChangeScreen function:

ChangeScreen(page) {
    page.addClass = page;
}

Then I can remove all of those lines: [class.selected]="CurrentPage == '...'"

2

4 Answers 4

60

You can use ngClass directive:

<div id="mydiv" [ngClass]="{'myCSSclass' : condition}"></div>

Simple as that! myDiv will have the class myCSSclass only when the condition evaluates to true. This condition can be set in your typescript file or in the template.

Sign up to request clarification or add additional context in comments.

Comments

18

Use Renderer

See here:https://angular.io/api/core/Renderer

and here:https://angular.io/api/core/Renderer#setElementClass

import { Renderer } from '@angular/core';

constructor(private render:Renderer) { }

ChangeScreen(event:any) {
 this.renderer.setElementClass(event.target,"selected",true);
}

In html:

<button class="general" (click)="ChangeScreen()">Global</button>

Or Render2:

See here:https://angular.io/api/core/Renderer2

and here:https://angular.io/api/core/Renderer2#addClass

import { Renderer2 } from '@angular/core';

constructor(private render:Renderer2) { }

ChangeScreen(event:any) {
 this.renderer.addClass(event.target,"selected");
}

In html:

<button class="general" (click)="ChangeScreen($event)">Global</button>

9 Comments

The class I am adding is .selected, not the page names
We are getting there :) ! Haha. The problem now is that you need to remove the .selected from all the previous ones. Because this will add the .selected class nicely, but it never removes previous ones
of course and you have removeclass function (and I an woman:) )
Haha my bad, I removed the "man" for you :) Would you mind adding the remove function to your example as well please? Then the answer is more complete
use this.renderer.removeClass('button', 'selected'); then add class to selected
|
4

While the solution with the Renderer works, I am proposing you create a data structure for your buttons

buttons: Array<{label: string}> = [
    {
      label: 'Global'
    },
    {
      label: 'Maintenance'
    },
    {
      label: 'Settings'
    },
    {
      label: 'Profile'
    },
    {
      label: 'Transactions'
    }
  ]

This can easily be iterated through using ngFor

<button 
  *ngFor="let button of buttons" 
  class="general" 
  (click)="ChangeScreen(button.label)" 
  [class.selected]="CurrentPage == button.label">
  {{ button.label }}
</button>

And only thing your ChangeScreen method would need is... the thing its already doing, setting the current page!

See stackblitz for an example of how this plays out.

1 Comment

I don't feel like this really solves the 'Angular add class dynamically' problem, but this is an awesome solution! Thank you!
3

Add a class in Angular programmatically with class

[class]="obj.param.name"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.