Ahmer Khan
Ahmer Khan

Reputation: 767

Change input value according to locale in AngularJS

I have a scenerio in which I have to change the number (that user puts in input field) according to its locale format. And I want that change to reflect in the HTML right away.

Like if user enter a number like 23.4 so it should change straight away to 23,4 in the ng-model and html view as well.

<input type="number" class="form-control" ng-model="vm.newPlayer.weight" ng-class="{'active' : vm.newPlayer.weight}">

This is the input field I have.

Upvotes: 0

Views: 386

Answers (2)

Junaid
Junaid

Reputation: 4926

Combining your ng-model, watch and toLocaleString you can achieve this. That is, bind input to ng-model, watch for changes in your component and then use toLocaleString to covert number to you desired locale. Us, German and other format are also available.

Upvotes: 0

gusaindpk
gusaindpk

Reputation: 1253

I think you can make use of watch and do the required things on the value change.

Here is a sample fiddle which may help you http://jsfiddle.net/user2314737/jmf6kL3n/

$scope.myName = "";
$scope.greeting = "h";
$scope.$watch("myName", function (newValue, oldValue) {
   //some custom code here
});

Upvotes: 1

Related Questions