Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

226
Vistas
Angular : how to properly set multiple conditions with ngClass

I am trying to create my own UI library using angular and Tailwind, I am facing an issue using ngClass :

  <button
    [ngClass]="{
      'px-4 py-2 text-sm': size === 'md',
      'px-4 py-2 text-base': size === 'lg',
    }"
  >
    My Button
  </button>

I want my class to be one of these above when the condition on the size is met. The problem is when the size equals 'md' and so the first condition is met : the class in the dom is only text-sm instead of px-4 py-2 text-sm

The ngClass documentations says :

keys are CSS classes that get added when the expression given in the value evaluates to a truthy value, otherwise they are removed

My understanding is that in my example :

  • The first condition is evaluated as true => the class is correctly being setup
  • Then the second condition is evaluated as false => px-4 py-2 text-base is being removed from the class as so the only one remaining is text-sm.

I think I was able to understand the problem, but since I'm new to angular and I would like to learn how to use properly ngClass, does anyone have a solution to solve this ?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Add the common classes to your regular class attribute, like this:

<button
    [ngClass]="{
      'text-sm': size === 'md',
      'text-base': size === 'lg',
    }"
   class="px-4 py-2"
  >
    My Button
  </button>
about 4 years ago · Juan Pablo Isaza Denunciar

0

Here you go, do it like this with multiple conditions:

[ngClass]="property  === value1 ? 'css-class-name1' : property  === value2 ? 'css-class-name2' : property  === value3 ? 'css-class-name3' :   'default-css'"
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda