jeewan
jeewan

Reputation: 1605

AngularJS Expression

I have a quick question on AngularJS: I have one property from JSON as

"icon-class": "home"

Now I need to do this in my html page:

<span class="{{sub.icon-class}}"></span> 

the sub is some other object here.

But inspecting the DOM, the value of the above expression is always zero (0). I guess the dash(-) is acting like a subtraction here.

How to do it I could not make it resolve.

Please help.

Upvotes: 0

Views: 50

Answers (2)

John Culviner
John Culviner

Reputation: 22944

You could do this:

<span class="{{sub['icon-class']}}"></span> 

But in general I'd avoid hyphens in variable names

Upvotes: 1

SomeKittens
SomeKittens

Reputation: 39522

In this case, you can use bracket notation to retrieve the value when the key has non-standard characters:

<span class="{{sub['icon-class']}}"></span> 

Read more about accessing properties at MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors

Upvotes: 3

Related Questions