13

Given a select list loaded with product options, I want the label to be in the format of option name and then price in parenthesis. So for example: "Product Option B ($1,432.12)". My option object has the properties "name" and "price". Price is numeric, I want it formatted with the currency filter. How would I do this? I'm thinking maybe a custom filter that takes a string and numeric value. Just not sure how I would be able to apply the filter within ng-options.

The following code just displays the name (not price):

<select class="form-control"
        ng-model="selectedOption"
        ng-options="option.name for option in category.options"></select> 
1
  • 1
    Couldn't you just decorate your model in the controller/directive with something like a displayName attribute? Commented May 2, 2014 at 22:20

1 Answer 1

30

This should work for you:

<select class="form-control"
    ng-model="selectedOption"
    ng-options="option.name + ' (' + (option.price | currency:'USD$') + ')' for option in options">
</select>

This will render a drop-down with items in this form: Item A (USD$14.00)

http://jsfiddle.net/6MYc3/

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

2 Comments

This is perfect but I have one problem. I have options which I want to show like this code - desc. But one of my options is code="", desc="" and in the result I get " - " but I want just to be blank as "". How can I achieve this please?
oh I see option.code as option.code==='' ? '' : (option.code + ' - ' + option.desc) for option in options

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.