search

Home  >  Q&A  >  body text

Parameter-based dynamic class display

I have some CSS classes that can have multiple names, how do I pass these names to ngClass?

The following is an example that doesn't work. How do I pass itemName to ngClass?

<div [ngClass]="isItem ? 'item {{itemName}}' : 'noItem {{itemName}}'">
</div>

P粉311423594P粉311423594438 days ago467

reply all(1)I'll reply

  • P粉627027031

    P粉6270270312023-09-15 00:35:19

    Template interpolation is not possible inside the [ngClass] directive, just remove the brackets and concatenate the variable with the string.

    <div [ngClass]="isItem ? 'item '+itemName : 'noItem'+itemName"></div>

    reply
    0
  • Cancelreply