opensas
opensas

Reputation: 63535

angular: programmatically format a number using the $locale information

I have included the corresponding locale file and it works fine. In any template I can do things like:

{{ value | number: 2}}

and it correctly formats the number according to the locale info.

Now I need to use the same locale info from javascript code in a controller to build a string.

I'm using a javascript component (a d3 graph to be precise) and I want to build strings to attache to it, so the template system is useless for this, but I'd like to take the locale configuration of numbers and dates from it.

So I'd nee something like this pseudocode:

var formattedValue = $local.format(value, { 'number': 2 });

Or something like that

Anyone knows how can I achieve that?

Upvotes: 0

Views: 1014

Answers (3)

opensas
opensas

Reputation: 63535

I could inject the filter like this:

presuApp.run(function ($rootScope, numberFilter) {
  var formattedValue = numberFilter(value, 2);
  [...]

It's just the name of the filter followed by th 'Filter' suffix.

Upvotes: 0

benek
benek

Reputation: 2178

Try this :

var formattedValue = $filter('number')(value,2);

Working : http://plnkr.co/edit/aC4p95y52YZyoUEdQVzo?p=preview

Upvotes: 1

Shreedhar
Shreedhar

Reputation: 5640

We can achieve this by implementing a filter.

var app = angular.module('app', []);
app.filter('yourFilter', function(){
  return function(string){
    // build the string whatever you are trying to achieve
    return newString; // return the string you achieved
  }
});

for reference, http://blog.trifork.com/2014/04/10/internationalization-with-angularjs/

Upvotes: 0

Related Questions