Created
July 21, 2015 14:53
-
-
Save bradberger/6ef1c7330816e4281e23 to your computer and use it in GitHub Desktop.
AngularJS Input Directive Formatters
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
"use strict"; | |
/** | |
* @description A simple formatter for <input type="checkbox"> fields. | |
* @usage <input type="checkbox" ng-model="myCheckbox" boolean> | |
*/ | |
angular.module("clls").directive("boolean", function() { | |
return { | |
require: "ngModel", | |
link: function(scope, element, attrs, ngModel) { | |
ngModel.$formatters.push(function(value) { | |
return !! value; | |
}); | |
} | |
}; | |
}); | |
/** | |
* @description A simple number formatter for <input type="text"> fields. | |
* @usage <input type="text" ng-model="myModel" number> | |
*/ | |
angular.module("clls").directive("number", function() { | |
return { | |
require: "ngModel", | |
link: function(scope, element, attrs, ngModel) { | |
ngModel.$parsers.push(function(value) { | |
return "" + value || 0; | |
}); | |
ngModel.$formatters.push(function(value) { | |
return parseFloat(value || 0, 10); | |
}); | |
} | |
}; | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment