0

I have this ngClass directive:

[ngClass]="{checkFav(fullArtist) ? 'action-btn-added' : 'action-btn-not-added'}"

Where checkFav is a function. I get this error:

Error: Template parse errors: Parser Error: Missing expected : at column 10 in [{checkFav(fullArtist) ? 'action-btn-added' : 'action-btn-not-added'}]

What am I doing wrong? I read a lot of answers, all of them suggesting things I already done like using quotes.

1
  • Wrong way around. {Classname: Condition} Commented May 2, 2020 at 11:43

2 Answers 2

3

You need change { to ( as

<div [ngClass]="(checkFav(fullArtist) ? 'action-btn-added' : 'action-btn-not-added')">Test</div>

But you should move logic to select class code to checkFav(fullArtist)

and only use <div [ngClass]="checkFav(fullArtist)">Test</div>

Demo https://stackblitz.com/edit/angular-rps3ma

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

Comments

1

You need remove the curly braces. The aren't required for ngClass in this context. Try the following

[ngClass]="(checkFav(fullArtist) ? 'action-btn-added' : 'action-btn-not-added')"

Comments

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.