0

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

10
  • 3
    Well, it's wildly invalid JSON, so it's no great surprise JSON.parse won't parse it... Commented Nov 25, 2016 at 15:37
  • @T.J.Crowder: How can i access the twitter property here? Commented Nov 25, 2016 at 15:38
  • 1
    There's probably another way to accomplish what you want. If you explain why you want to access twitter and yahoo in your conditional class, we can probably give you some better options. Commented Nov 25, 2016 at 15:38
  • 1
    Explain "dynamic templating". Angular does that beautifully, and I've never had to attempt to access the ng-class in my experience. Commented Nov 25, 2016 at 15:40
  • 1
    @Damon: It's an expression. Angular's ngClass directive understands that syntax, and will give the element the class twitter if the expression is true and won't if it's false. Commented Nov 25, 2016 at 15:58

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

typeof ng-class="{twitter:2 < id , yahoo :2 >id}" gives me an object.. is there a way to access the twitter property here.
@Shane: I have no idea what you mean by typeof ng-class. I can't imagine any context in which you could write that and have it be valid code. Perhaps an minimal reproducible example in the question would help.
typeof "{twitter:2 < id , yahoo :2 >id}" returns me an object instead of string... here is a fiddle jsfiddle.net/9wg1trx1/1
I am trying to replace this {{ind}} with 0, 1, 2, 3, 4... so on
@Shane: What you're accessing there is an attribute on an element. You can get the text of the attribute via getAttribute, e.g. val.getAttribute("ng-class'): jsfiddle.net/9wg1trx1/2 You can update the value of the attribute with setAttribute, but I very much doubt this is the right way to go about what you're doing, unless Angular hasn't yet been run on the elements in question (in which case, it should be fine). If Angular's already been run on the elements in question, it has presumably already tried to use that ng-class attribute with the {{ind}} in it.
|
0

Please add quotes and () for values of object like this:

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

With this update you not see Uncaught SyntaxError again!

2 Comments

Code-only answers aren't helpful. Explain what you did, how / why it solves the problem.
Moreover, this has nothing to do with the problem or the question. The () there are completely unnecessary and change nothing.
0

Works as expected as you can see in this Plunkr

When interpreted by Angular, ngClass assigns class values to class element attribute.

when i try to access it like elem["ng-class"]["twitter"] does not work.

If you want to check if the DOM element has "twitter" class simply do it by querying the DOM.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.