'AngularJS not interpreting the condition expression correctly
I'm using angularjs the ng-style to format the text with a color if the condition is met, but for some reason, the Lexer gives an error:
Error: [$parse:lexerr] Lexer Error: Unterminated quote at columns 23-24 ['] in expression [asignadap.tipoFlag === '].
This is my code:
ng-style="object.type == 'type 2' && {'color':'#e30754'}"
If i substitute with a number it works.
I don't quite understand why with text the condition doesn't work, i have tried these variations of the condition:
ng-style="object.type == '"type 2"' && {'color':'#e30754'}"
ng-style="object.type == \\"type 2\\" && {'color':'#e30754'}"
How i compare the variable with text correctly?
From the console, the error on how it's interpreting the expression:
ng-style="object.type === '" type="" 2"'="" &&="" {'color':'#e30754'}"=""
Solution 1:[1]
Imho code like a && b is not always good when a and b can be of different types. Plain JS allows too much, angularjs parser allows much less. In your case just use ternary operator:
object.type == 'type 2' ? {color:'#e30754'} : {}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Petr Averyanov |
