Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format input value in Angularjs

I'm trying to write a directive that automatically formats a number in the <input> but the the model isn't formatted. Getting it to work is fine, on load the value in the input is displayed as 1,000,000 and 1000000 in the controller, however when typing only the ngModel.$parsers function fires. The only time when the ngModel.$formatters fire is when the directive gets loaded and when the value is 0.

How can I make it work on keypress(I've tried binding to keypress/keyup but it doesn't work).

Here's my code:

angular.module('myApp.directives', []).directive('filterInput', ['$filter', function($filter) {     return {         restrict: 'A',         require: 'ngModel',         link: function(scope, element, attr, ngModel) {              ngModel.$parsers.push(function fromUser(text) {                 return parseInt(text.replace(",", ""));             });              ngModel.$formatters.push(function toUser(text) {                 console.log($filter('number')(text));                 return ($filter('number')(text || ''));             });          }     }; }]); 
like image 593
Jon Snow Avatar asked Nov 10 '13 13:11

Jon Snow


People also ask

What is ngInit?

The ngInit directive allows you to evaluate an expression in the current scope. This directive can be abused to add unnecessary amounts of logic into your templates. There are only a few appropriate uses of ngInit : aliasing special properties of ngRepeat , as seen in the demo below.

What is Ng blur in AngularJS?

The ng-blur directive tells AngularJS what to do when an HTML element loses focus. The ng-blur directive from AngularJS will not override the element's original onblur event, both the ng-blur expression and the original onblur event will be executed.

What is Ng pattern in AngularJS?

The ng-pattern Directive in AngularJS is used to add up pattern (regex pattern) validator to ngModel on input HTML element. It is used to set the pattern validation error key if input field data does not match a RegExp that is found by evaluating the Angular expression specified in the ngpattern attribute value.


1 Answers

Here is working example where we use unshift:

angular.module('myApp.directives', []).directive('format', ['$filter', function ($filter) {     return {         require: '?ngModel',         link: function (scope, elem, attrs, ctrl) {             if (!ctrl) return;               ctrl.$formatters.unshift(function (a) {                 return $filter(attrs.format)(ctrl.$modelValue)             });               ctrl.$parsers.unshift(function (viewValue) {                 var plainNumber = viewValue.replace(/[^\d|\-+|\.+]/g, '');                 elem.val($filter(attrs.format)(plainNumber));                 return plainNumber;             });         }     }; }]); 

The HTML seems:

<input type="text" ng-model="test" format="number" /> 

See Demo Fiddle

Hope its help

like image 150
Maxim Shoustin Avatar answered Sep 23 '22 15:09

Maxim Shoustin