4

I have this code

    <div *ngFor="let d of getDays(); let i = index" class="day hr left" (click)="pick(d, h)" [class.white]="days[d].indexOf(h) > -1">
</div>

It works fine but my question is how do I add another class name in this part [class.white]="days[d].indexOf(h) > -1"? I have another class called .redborder{border 1px solid red}. I tried this but didn't work [class.white.redborder]="days[d].indexOf(h) > -1". What else am I supposed to do?

1 Answer 1

7

You can use the ngClass directive:

<element [ngClass]="{'white redborder' : days[d].indexOf(h) > -1}"></element>

For elseif I guess you can use this:

<element [ngClass]="days[d].indexOf(h) > -1 ? 'white' : 'redborder'"></element>
Sign up to request clarification or add additional context in comments.

2 Comments

Wow thnx man, it worked @PierreDuc. How about multiple conditions as in the case of else if?
@SamZiggler I've updated my answer to show else if. It is however advised to use as little logic inside your templates, and use functions or getters in your component class. This helps refactoring and testing, and increases readability

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.