Skip to content

Instantly share code, notes, and snippets.

@bradberger
Created July 21, 2015 14:53
Show Gist options
  • Save bradberger/6ef1c7330816e4281e23 to your computer and use it in GitHub Desktop.
Save bradberger/6ef1c7330816e4281e23 to your computer and use it in GitHub Desktop.
AngularJS Input Directive Formatters
"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