1

I'm iterating over a collection of objects using ng-repeat to create multiple anchor tags and I want to apply a class name equal to the name value of each object plus the word "icon". E.g. for the object { "name" : "square" }, I'd like the tag to have the class="square-icon".

What is the best way to accomplish this?

HTML

<div id="container">
  <a ng-repeat="item in items" ng-class=??(I'm not sure what to put here)?? 
  </a>
</div>

JSON Object example

[{ name: "square" }, 
{ name: "hexagon" }, ... ]

1 Answer 1

2

The simplest way is to add a class with plain class attribute.

Try <a ... class="{{someObject.name}}-icon">.

For flexibility, you might want to implement custom function which returns a proper ng-class value.

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

1 Comment

Great, thank you for both suggestions. For my needs, the simpler solution was sufficient.

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.