Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use an angularJs filter on items label within ng-options

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> 
like image 281
Ryan Langton Avatar asked May 02 '14 21:05

Ryan Langton


1 Answers

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/

like image 165
Sebastian Avatar answered Sep 20 '22 16:09

Sebastian