-1

How do I correctly display key value pairs using AngularJS?

Here is my HTML:

<ul class="item-property-list">
    <li ng-repeat="(key, value) in item.item_properties">
    {{key.key}} <strong>&nbsp;{{value.value}}</strong>
    </li>
</ul>

Here is the JSON data:

"item_properties":
          [
                        {
               "key": "Size",
               "value": "S"
            },
                        {
               "key": "Color",
               "value": "Red"
            }
         ]

Here is the output:

S Red

I would like to have:

Size: S
Color: Red
1

2 Answers 2

1

Just access it directly:

 <li ng-repeat="element in item.item_properties">
    {{element.key}} <strong>&nbsp;{{element.value}}</strong>
    </li>
Sign up to request clarification or add additional context in comments.

2 Comments

Why didn't I think of that! Thank you!
or {{value.key}} <strong>&nbsp;{{value.value}}</strong> using original (key,value) pair
0

(key, value) is for iterrating over an object that has keys and values. item_properties is an array, so repeat normally. Then take the repeated object and access your "key" property on it. Note that your "key" property is not a key in the KVP sense. In {color:'red'} color is a true key.

<ul class="item-property-list">
    <li ng-repeat="item_property in item.item_properties">
    {{item_property.key}} <strong>&nbsp;{{item_property.value}}</strong>
    </li>
</ul>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.