1

I am tying to add filter in input field but it gives error ..I want if I type 1k in input field it show it text 1,000 in input field (value)..In other words if I type 1k ..it convert 1k or remove / change 1k to 1000.

I try like that but not work ..here is my code

http://plnkr.co/edit/6E4pe6RO8WwhINTpEf27?p=preview

.filter('toNumber', function($filter) {

  var numberFilter = $filter('number');

  var dict = {
    k: 1000,
    l: 100000
  };

  var regex = /^([-+]?[0-9]*\.?[0-9]+)([kl])$/;

  return function(val, fraction) {
    var match;
    if(angular.isString(val) && (match = val.match(regex)) ) {
      val = match[1] * dict[match[2]];
    }

    return numberFilter(val, fraction || 0);

  };
});

3 Answers 3

1

In your case, you shoulnd't use filter as it is only to format value, not to change it. In addition, ngModel is two-way binding and only support variable affectation.

You can use a directive instead :

JS (add improvement suggested by Cameron Morrow :)

.directive('toNumber', function(numberFilter) {
    var dict = {
      k: 1000,
      l: 100000
    };

    var regex = /^([-+]?[0-9,]*\.?[0-9]+)([kl])$/;

    return {
      require: 'ngModel',
      link: function link(scope, element, attr, ngModel) {
        scope.$watch(attr.ngModel, function(val) {
          var match;
          if(angular.isString(val) && (match = val.match(regex)) ) {
            val = match[1].replace(/,/g , "") * dict[match[2]];
            scope[attr.ngModel] = numberFilter(val, 0);
          }
        })
      }
    }
})

HTML

<div ng-controller='cntrl'>
     <input type='text' to-number ng-model='user'> 
</div>
Sign up to request clarification or add additional context in comments.

4 Comments

please share plunker
I make plunker that work fine but there is problem "," not come..mean 1k ..should display 1,000..and 10k display 10,000
Right :) should be better now : plnkr.co/edit/SIDsjUWuCs5HKdarfEkf?p=preview
This is a really good solution, but I noticed it breaks after the first k because of the commas the number filter adds. I made a slight change here to resolve this: plnkr.co/edit/ZBPyh8Iu6sLjYo7czQfB?p=preview
0

I would wire up an ng-change instead.

<div ng-app="app">
    <div ng-controller="ParentCtrl">
        <input ng-model="inputValue" ng-change="inputValueChanged()" />
    </div>
</div>

Javascript

angular.module('app', [])

function ParentCtrl($scope){
    $scope.inputValueChanged = function(){
        if($scope.inputValue === '1k'){
            $scope.inputValue = '1000';
        }
    }
}

Fiddle: http://jsfiddle.net/unbbmk7m/

4 Comments

your answer is correct ..but it not work on 2k, 1.2k ...or 1L
I think I need to use filter ..my filter is correct but it not set the value in input field
No just put the filter logic in the ng-change handler.
I only provided an example of what could be done, you will have to flush out the rest.
0

controller:

angular.module('app',[]).controller('cntrl',function($scope){
    $scope.user ="";
    var dict = {
        k: 1000,
        l: 100000
      };

    var regex = /^([-+]?[0-9]*\.?[0-9]+)([kl])$/;

    $scope.$watch('user', function() {
      var match;
      var val = $scope.user;
      if(angular.isString(val) && (match = val.match(regex)) ) {
        $scope.user = match[1] * dict[match[2]];
      }
    });
});

and HTML:

<div ng-controller='cntrl'>
      <input type='text' ng-model='user'> 
</div>

Comments

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.