Skip to main content

thatThe code below works for me well, but if I put inonly when the input type = "number" does not work meis text, and I needit doesn't work when the input is 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 codepen:code work. The code is also on http://codepen.io/fernandooj/pen/EgmQmJ?editors=1011CodePen.

that works for me well, but if I put in the input type = "number" does not work me, and I need the input is type 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 } }); 

and the codepen: http://codepen.io/fernandooj/pen/EgmQmJ?editors=1011

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.

Source Link

angular directive does not work with input type = "number"

that works for me well, but if I put in the input type = "number" does not work me, and I need the input is type 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 } }); 

and the codepen: http://codepen.io/fernandooj/pen/EgmQmJ?editors=1011