1

I have an array of pairs of amounts and currencies. The latter not just 'USD' or 'EUR', but is an object that contains formatting rules for that currency. I've written a filter for formatting the amount, but it only works if I hard code the formatting arguments:

// arguments: currency symbol, is symbol prefix, decimal symbol, digit group symbol
{{ entry.amount | currency24: '$',true,'.',',' } //USD example
{{ entry.amount | currency24: '€',false,'.',',' } //EUR example

What I'd like to be able to do is put the whole object in there like so:

{{entry.amount | currency24: entry.currency }} // currency object holds all the formatting parameters, but I could just as well pass them one by one

How can I achieve this?

EDIT: entry is not a part of $scope. It come from an ng-repeat:

ng-repeat="entry in entries"

That's why I'm having problems passing it to a filter.

1
  • 1
    Can you show your currency24 filter implementation ? Commented Nov 13, 2014 at 21:29

1 Answer 1

1

You are able to pass any object to filter just like you mentioned.

HTML markup stays the same you wanted:

<p ng-repeat="entry in entries">
  {{entry.amount | currency24: entry.currency }}
</p>

And the following code does what you asked for:

.controller('DemoController', function($scope) {
    $scope.entries = [{
      amount: 35,
      currency: {
        symbol: '$'
      }
    }, {
      amount: 40,
      currency: {
        symbol: '€'
      }
    }];
})

.filter('currency24', function() {
  return function(amount, currencyObject) {
    return amount + currencyObject.symbol;
  };
});

And check out this plunker.

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

3 Comments

Forgot to mention that entry is not in $scope, rather it's from an ng-repeat: ng-repeat="entry in entries". entries, is in $scope, though. I edited my question to reflect this.
that shouldn't be the problem, i guess. i updated the answer and plunker. did i misunderstand your question?
You're correct. I found that my problem was in another part of my application which caused that the entry.currency object was actually undefined. Thanks a lot for your help.

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.