0

I'm looking for a directive that would allow me to do things like Kendo-UI's numeric textbox control ( http://demos.telerik.com/kendo-ui/numerictextbox/index ).

I know they have angular directive translators but I'd like to avoid the weight of that.

Basically: decimal, currency, comma, number only in the actual text box. I know angular has filters but i can't seem to find them working actually in the textbox.

Has anyone ran across something like this or have suggestions about how to do this in angular?

3
  • Just use the html5 controlls, almost all new browsers support them. Commented Jun 4, 2014 at 14:41
  • it doesn't do the decimal / currency features Commented Jun 4, 2014 at 15:17
  • I did find this ... github.com/fiestah/angular-money-directive only handles currency though Commented Jun 4, 2014 at 15:18

1 Answer 1

2

Building on @1st4ck suggestion, i implemented my own directive that uses angular core filters but displays them as if it were a textbox :)

Directive

var module = angular.module('components.number', []);

module.directive('numberInput', function ($timeout, $filter) {
    return {
        restrict: 'EA',
        templateUrl: "common/components/number/number.tpl.html",
        scope:{
            ngModel: "=",
            format: "=",
            min: "=",
            max: "=",
            step: "=",
            decimalPlaces: "=decimals"
        },
        link: function($scope, $elm, $attrs) {
            $scope.showNumber = false;
            $scope.numberBlurred = function(){
                $scope.showNumber = false;
            };

            $scope.textBlurred = function(){
                $scope.showNumber = true;
            };

            $scope.textFocused = function(){
                $scope.showNumber = true;
                $timeout(function(){
                    $elm.find('input[type=number]').focus();
                }, 50)
            };

            $scope.$watch('ngModel', function(){
                var formatted;

                if($attrs.format === "percent"){
                    formatted = $filter("number")($scope.ngModel, 0);
                    if(formatted){
                        formatted = formatted + "%";
                    }
                } else if($attrs.format === "decimal"){
                    formatted = $filter("number")($scope.ngModel, $attrs.decimalPlaces || 2);
                } else {
                    formatted = $filter($attrs.format)($scope.ngModel);
                }

                $scope.formatted = formatted;
            }, true);

        }
    };
});

return module;

Template ( common/components/number/number.tpl.html )

<input type="number"
       ng-model="ngModel"
       class="form-control"
       ng-show="showNumber"
       ng-blur="numberBlurred()" />

<input value="{{formatted}}"
       class="form-control"
       ng-click="textFocused()"
       ng-hide="showNumber" />

Usage

<number-input ng-model="myModel" format="currency">
<number-input ng-model="myModel" format="decimal" decimals="5">
<number-input ng-model="myModel" format="number"> // mainly for thousands

The timeout usage to focus is a bit odd but oh well. Does the job!

Whats great about this solution, it shows the user the UI formatted but preserves the raw values for formatting on the client while using angular native filters :)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for sharing @amcdnl ! Looks great but I'm a little lost about the implementation process. Could you please place an example of your solution?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.