Sagar Suryawanshi
Sagar Suryawanshi

Reputation: 195

conditional currency sign with angular filter

$scope.countries = [{name: 'UNITED KINGDOM', id: 1, sign:"£", 'val':2300987 },{name:    'AUSTRIA', id: 2, sign:"€", 'val':6703482 }
// In HTML
   <tr ng-repeat="country in countries">
        <td class="td-border">{{country.val| currency}}</td>
   </tr>

I need angular filter which will take care of currency conditionally as per above object..

Upvotes: 2

Views: 1279

Answers (4)

Pankaj Parkar
Pankaj Parkar

Reputation: 136154

From Angular Docs

Filter definition {{ currency_expression | currency : symbol : fractionSize}}

Simply you could add parameter to filter which would be sign of your currency.

Markup

<tr ng-repeat="country in countries">
    <td class="td-border">{{country.val| currency: country.sign}}</td>
</tr>

Upvotes: 3

vorillaz
vorillaz

Reputation: 6276

The filtering of angular could be bind on the repeater. Using a separate model to store the current currency is super easy:

$scope.currency = "£";

Filtering on the repeater will be quite easy then:

<tr ng-repeat="country in countries  | filter : { sign : currency || ''}">
                <td class="td-border">{{country.val}}</td>
            </tr>

Working demo: http://jsfiddle.net/CBgcP/692/

Upvotes: 0

ukn
ukn

Reputation: 1793

First, if you want the filter to format your currency you will have to pass the country object or the sign and the val to it, otherwise you can't add the sign with the filter.

   .filter('currency', [function() {
    return function(countryObj) {    
            return countryObj.val + countryObj.sign;
        }
    };
    }])

Upvotes: 0

Nikhil Aggarwal
Nikhil Aggarwal

Reputation: 28455

I believe you need to filter the rows based on currency. You can achieve that by simply updating the code to following

// In HTML
<tr ng-repeat="country in countries | filter : {sign : currency}">
    <td class="td-border">{{country.val}}</td>
</tr>

Upvotes: 0

Related Questions