1

I want to write code like this note: the classes in the class (style13 style121 style541) property i don't have any control on it and i want to add other class from variable on the TS

example:

<div class="style13 style121 style541" [class]="varInTheCode" [class.other-style3]="true">
...
<div>

TS

export class MyDataTableComponent implements OnInit, OnChanges {
    varInTheCode:string = "other-style1 other-style2";

}

I hope the result in the browser will be like this:

<div class="style13 style121 style541 other-style1 other-style2 other-style3" ...>
...
<div>

but the problem always [class] replace the class!

can i do it?

1
  • Use [ngClass]. It won't replace existing class. It will add new class in that. Commented Jul 26, 2020 at 3:03

1 Answer 1

2

You have two options here:

  • Use Ivy(Angular 9^): it should work out of the box there

  • Use [ngClass] directive instead of [class] property binding

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

2 Comments

what is Ivy(Angular 9^)??
Ivy is the code name for Angular's next-generation compilation and rendering pipeline. It's used by default starting from Angular 9

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.