0

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.

2 Answers 2

3

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

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

1 Comment

superb, thanks. It worked. I understand the logic now. array[key] seems working. Thank you so much. I am accepting and up-voting your answer.
1

You could do this:

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

But in general I'd avoid hyphens in variable names

2 Comments

You have a syntax error - the dot shouldn't be there.
ya, looks like it is a syntax error. Hey, guys not its working. Thank you so much.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.