Shane Shane - 11 days ago 5
Javascript Question

JSON.parse fails in converting it to object

I am getting a

ng-class
like this in my dom, how can i access the
twitter
and
yahoo
attributes here.

ng-class="{twitter:2 < id , yahoo :2 >id}"


typeof ng-class
is
object
, but when i try to access it like
elem["ng-class"]["twitter"]
does not work.

While doing
JSON.parse(obj)
also fails.


Uncaught SyntaxError: Unexpected token t in JSON at position 1

Answer

What you have there isn't an object, it's a pair of conditionals in an ngClass directive: The element will have the class twitter if 2 < id is true, and will have the class yahoo if 2 > id is true (it will have neither of them if id == 2).

Since it's not an object, there's no property to access.