9

I'm trying to add variable-driven class(es) to an element, along with other classes using ngClass.

Example:

// inputExtraClass = 'form-control-sm'

// Working
<input class='form-control' [ngClass]="inputExtraClass" placeholder="Working">

// Not working
<input class='form-control' [ngClass]="{inputExtraClass: true}" placeholder="Not working">

Plunker

2
  • you should be using component level style class or add selector before the class to ensure that it is applied. answer Commented Nov 3, 2017 at 16:44
  • The class that is stored in the inputExtraClass variable is a bootstrap class, not a custom one. Commented Nov 3, 2017 at 16:54

4 Answers 4

12

Providing a kind of complete answer to your question,

Do: <input class='form-control' [ngClass]="{'inputExtraClass': true}" placeholder="Not working">

and if you want more than one class or switch between classes you can also do something like

<input class='form-control' [ngClass]="{'inputExtraClass': true, 'notInputExtraClass': !true }" placeholder="Not working">

this way above, it will be either one class or the other

You can also aply any other variation you like using this, or make a property in your component like this one:

toggleClass: boolean = true;

and in your html:

<input class='form-control' [ngClass]="{ 'inputExtraClass': toggleClass, 'notInputExtraClass': !toggleClass }" placeholder="Not working">

same idea, and then you could create a method and change the toggleClass property or whatever :) hope it helped

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

6 Comments

inputExtraClass is a variable in the component (inputExtraClass = 'form-control-sm'). If I do 'inputExtraClass': true, it would mean inputExtraClass is the actual class name, no?
Should work as well, for example in one of my applications i have: [ngClass]="{ dropdown: !shouldDisplayDropdown }" and it's defined in the component like: dropdow: string ="dropdown"; and sorry that i can't play with your plunkr ATM, have a bad connection right now
Can you update this Plunker?
the pluker is not loading for me, but i will try
if you don't mix quoting classes with the ones coming from the component property it will work @MohammadAli
|
5

You can use the "array form" (https://angular.io/api/common/NgClass#description) in this way:

cond1 = true;
cond2 = false;
smClass = 'form-control-sm';
xlClass = 'form-control-xl';

<input class='form-control' [ngClass]="[ smClass, cond1 ? 'form-control-lg' : '', cond2 ? xlClass : '' ]">

This will be:

<input class="form-control form-control-sm form-control-lg">

Comments

0

You can use expressions inside ngClass:

<input class='form-control' [ngClass]="(addClassIfTrue) ? 'inputExtraClass' : ''" placeholder="Working">

2 Comments

Similar response to Alejandro, inputExtraClass is a variable in the component, not the actual name of the class so putting it in single quotes would not apply the actual class
That string is supplied by the user (empty string if not supplied), so cannot set it to null. Also, doing it this way [ngClass]="inputExtraClass" will not allow me to set multiple classes via ngClass.
0

If someone wants to directly use the class name with the dynamic variables, without any conditions, then they can use this way.

<div class="card-table second" ngClass="{{source}} {{currentMode}}">

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.