Format value

11 views
Skip to first unread message

Nilson Uehara

unread,
Aug 22, 2016, 3:46:49 PM8/22/16
to AngularJS
I'm trying to use the cord-plugin-globalization plugin but I'm bumping my little experience with JavaScript.

xhtml:

<div>{{ item.valor | formataValor }}</div>


filter.js:

app.filter('formataValor',function(){
   return function(valor){
       console.log('globalization: '+valor)
       navigator.globalization.numberToString(
           valor,
           function (number) {
               console.log('numero formatado:' + number.value);
               return number.value;
           },
           function () {
               console.log('erro de formatacao de valor');
           },
           {type: 'currency'}
       );
   }
});

The console output is:

"globalization: 123.4" 
"numero formatado: 123,40"

but the screen is blank.


Sander Elias

unread,
Aug 23, 2016, 2:57:51 AM8/23/16
to AngularJS
Hi Nilson,

Your filter is not returning anything. Your callback function has a return, but that doesn't propagate to your filter function. You need to rewrite your filter to s $statefull one, and handle the async nature of navigator.globalization.numberToString for yourself. 

It is by far easier en faster to handle this in your controller/service before displaying it in your template.

Regards
Sander

Nilson Uehara

unread,
Aug 23, 2016, 7:58:40 AM8/23/16
to AngularJS
Sanders, thanks for your time!

I am novice in the javascript.
I do not know working with this asynchronous method. How would the correct way?

I do not know if I can do the formatting for the controller, because what I'm showing is a list of values:

<ion-list>
  <ion-item ng-repeat="item in items" ng-click="click(item)">
    <div class="row">
      <div class="col">{{item.valor | formataValor}}</div>
    </div>
  </ion-item>
</ion-list>

Besides, I'll need this value later to do calculations, so I can not convert it to String (formatted value)

Regards,
Nilson Uehara

Sander Elias

unread,
Aug 23, 2016, 9:00:59 AM8/23/16
to AngularJS
Hi Nilson,

In your controller you can do something like this:

$scope.items.forEach(function (item) {
     navigator
.globalization.numberToString(item.valor, function (number) { item.valorFormatted = number}, function () { console.log('erro de formatacao de valor'); }, {type: 'currency'})
});

then in your view:
<div class="col">{{item.valorFormatted }}</div>

When you do a calculation, you need to update the formatted version to, but that's not a big deal.

Regards
Sander

Nilson Uehara

unread,
Aug 23, 2016, 9:25:43 AM8/23/16
to AngularJS
Sander,

I did this on a test, but I thought there was a more elegant way to format on the view.

Anyway, thank you very much!

Regards
Nilson Uehara
Reply all
Reply to author
Forward
0 new messages