Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is AngularJS complaining about an unexpected token in an expression when I try to use a string?

I have the folowing attribute on a div: ng-show="state.name === 'index'". I've also tried ng-show='state.name === "index", but I keep getting the following error:

Syntax Error: Token '"index"' is an unexpected token at column 16 of the expression [state.name === "index"] starting at ["index"].

Why?

like image 516
bigblind Avatar asked Oct 21 '12 14:10

bigblind


People also ask

What is unexpected token error in JavaScript?

Unexpected Token errors belong to SyntaxErrors. This error occurs when we try to call the code with the extra or missing character which does not belong to JavaScript family. In this tutorial, we will try to fix the Unexpected Token error.

How to remove unnecessary use of any token in JavaScript?

Similarly, unnecessary use of any token will throw this type of error. We can remove this error by binding by following the programming rules of JavaScript.

What are the tokens reserved by the JavaScript engine?

Tokens: All the operator (+, -, if, else…) are reserved by the JavaScript engine.So, it cannot be used incorrectly .It cannot be used as part of variable names. Line terminators: A JavaScript code should end with semi-colon (;). Control characters: To control code, it is important to maintain braces ( { }) in a code.

Why can't I add colons to forms in AngularJS?

This is a problem caused by AngularJS using jQuery and appears to be a known limitation (see issue 13771 ); jQuery treats colons as special selectors so ids with colons break the parser. Your options are to either override the form directive or move the apex:form tag outside of your Angular app, which is what I did.


1 Answers

ng-show takes an "AngularJS statement." This type of statement only has an == operator, but this operator behaves like ===. It's a bit confusing, but handy in that you cannot shoot yourself in the foot with weird type coercion.

like image 94
btford Avatar answered Sep 23 '22 17:09

btford