AlexFF1
AlexFF1

Reputation: 1283

Angular [ngClass] condition and function combined

Is it possible to combine the conditional statement and the function together in the [ngClass]

 [ngClass]="tagType.tagColor ? 'tags-button' : 'tags-button tag-colorless' &&  getStyleClasses()"

I get an error on syntax above, is there a correct way to include the conditional statement and the function together? The function should just evaluate on its own without a condition

Upvotes: 0

Views: 131

Answers (1)

Christian Vincenzo Traina
Christian Vincenzo Traina

Reputation: 10464

You are evaluating 'tags-button tag-colorless', which is always true, so the result is always substituted by getStyleClasses().

In JavaScript, the last value of an evaluation get returned, for example:

var x = true && 'foo'; 

assigns the string foo.

var x = false && 'bar';

assigns the boolean false, since the first value is falsy and the second value doesn't get evaluated.

In your case, 'tags-button tag-colorless', is a string, and not-empty strings are always truthy, so it's like the first example above.

Maybe you meant concatanation, so substitute && with +

Upvotes: 1

Related Questions