0

The code below works only when the input type is text, and it doesn't work when the type is number.

<div ng-app="myApp" ng-controller="myCtrl as model">
 <input type="text"  ng-model="cero" ng-decimal   >   
</div> 


angular
    .module("myApp",[])
    .controller('myCtrl', function($scope){
        var model=this;
    })
    .directive('ngDecimal', function ($parse) {
        var linkFunction =function(scope, element, attrs){     
            element.bind("keypress", function(event) {
                if(event.which === 13) {    
                    scope.$apply(function(){
                        scope.$eval(attrs.format, {'event': event});
                        if(scope.cero===undefined || scope.cero===''){
                            scope.cero="0.", 
                            event.preventDefault();
                        }else{

                        }
                    });
                }
            });
        };
        return{
            restrict : 'A',
            scope:{
                cero: '=ngModel'
            },
            link: linkFunction
        }
    });

What I need help with is changing the type to number and still making the code work. The code is also on CodePen.

5
  • 1
    You didn't describe your problem or ask a question. Commented Oct 4, 2016 at 14:49
  • maybe you should try with scope.cero="0," Commented Oct 4, 2016 at 14:50
  • What you are trying to do with the directive. Can you elaborate your question? Commented Oct 4, 2016 at 14:51
  • @GangadharJannu well my question is: how I can make that directive to work me to change the input of text to number Commented Oct 4, 2016 at 14:52
  • @asdf_enel_hak , thanks for your answer, but dont work Commented Oct 4, 2016 at 14:54

2 Answers 2

1

Updated pen : http://codepen.io/anon/pen/QKOVkP?editors=1011

Works with number, constraint being you cannot assign

scope.cero = "0." // string value

to a type="number" so replace it with the minimum number you want to assign, maybe

scope.cero = parseFloat("0.01")  // parseFloat("0.") won't work
Sign up to request clarification or add additional context in comments.

2 Comments

yes perfect, But the condition, so I did not eliminate what the user has written but that no longer works, you know that?
updated plunker with alert() , working both ways, also check the if condition scope.cero === null has been added to the conditions - codepen.io/anon/pen/QKOVkP?editors=1011
1

In the else condition add this.

scope.cero = parseFloat(scope.cero).toFixed(2);

Convert string to decimal

Here is the code: working code

1 Comment

thanks for this code, but if i change the input to type number dont work codepen.io/fernandooj/pen/amVaQm?editors=1011

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.