A Pen by Anonasaurus Rex on CodePen.
Created
July 10, 2014 02:43
-
-
Save Maxim-Filimonov/f0d73935492e3ac0b3f9 to your computer and use it in GitHub Desktop.
A Pen by Secret Sam.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<section ng-app="myApp" ng-controller="MainCtrl"> | |
<h4 class="text-center">AngularJS Numeric Value Widget</h4> | |
<div class="well entry"> | |
<label>Activity entry</label> | |
<input type="text" ng-model="activityUnitValue" name="unitValue" integer="" numeric="" positive=""> | |
</div> | |
</section> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var app = angular.module('myApp', []); | |
app.controller('MainCtrl', function($scope) { | |
}); | |
app.directive('numeric', function () { | |
return { | |
require: '?ngModel', | |
link: function (scope, element, attrs, modelCtrl) { | |
return modelCtrl.$parsers.push(function (inputValue) { | |
var transformedInput; | |
if (inputValue == null) { | |
return ''; | |
} | |
transformedInput = inputValue.replace(/[^0-9]/g, ''); | |
if (transformedInput !== inputValue) { | |
modelCtrl.$setViewValue(transformedInput); | |
modelCtrl.$render(); | |
} | |
return transformedInput; | |
}); | |
} | |
}; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.entry { | |
width: 300px; | |
margin: 10px auto; | |
text-align: center; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment