1

I have just started a new project with angular and I learnt abount *ngIf. I would like to use *ngIf in order to add / remove a class from a div

Before click

<div class="done">Foo</div>

After click

<div class="notdone">Foo</div>
1
  • 1
    use ngClass instead of ngIf read from here https://angular.io/api/common/NgClass Commented Oct 30, 2019 at 21:29

1 Answer 1

1

You have different ways of approaching this.

<div [ngClass]="{'done': conditionToDone, 'notdone': !conditionToDone}">Foo</div>

Where done or notdone are added via a condition.

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

3 Comments

Thanks a lot! It solved my problem. I have to say that you left ' " ' after '{' and before '}' and got an error, but solved quickly!
Edited my answer. Happy to help.
Another suggestion: [ngClass]="condition ? 'done' : 'notdone'".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.