Angular 2 ngClass: Get Got interpolation ({{}}) where expression was expected when trying to display json on html

joeCarpenter picture joeCarpenter · Oct 7, 2016 · Viewed 8.1k times · Source

I'm new to Angular 2 and was hoping to get some help from the community. I'm currently trying to implement a dynamic/conditional implementation of ngClass in a <tr> element of my html view. The trufy used is a variable and its original value comes from a JSON object set on my Componenet:

<td [ngClass] = "{weak : {{jsonInMyComponenet.element}}, neutral : !{{jsonInMyComponenet.element}}}"  ></td>

When I use the code above I get this error:

Got interpolation ({{}}) where expression was expected

If I remove the curly brackets I get no errors but the page doesn't render the element, so I can't see the class implementation of weak nor neutral. What am I'm doing wrong?

Answer

G&#252;nter Z&#246;chbauer picture Günter Zöchbauer · Oct 7, 2016

Don't use [...] and {{...}} together. Either the one or the other.

<td [ngClass] = "{'weak' : jsonInMyComponenet.element, 'neutral' : !jsonInMyComponenet.element}"  ></td>

{{...}} is for string interpolation. [...] interprets the value as expression.