Skip to content

Instantly share code, notes, and snippets.

@natasv
Created September 21, 2017 07:34
Show Gist options
  • Save natasv/492bc3847dc5a9bd4651350884aca836 to your computer and use it in GitHub Desktop.
Save natasv/492bc3847dc5a9bd4651350884aca836 to your computer and use it in GitHub Desktop.
function digitNegativeNumbers(): any {
return {
restrict: 'A',
require: 'ngModel',
link(scope: IScope, element: HTMLElement, attrs: any, ngModelCtrl: any) {
function inputValue(val: string) {
if (val) {
const transformedValue = val.replace(/([^-\d.]+)?((-{0,1}\d*\.?\d*)(.*)?$)/, '$3');
if (transformedValue !== val) {
ngModelCtrl.$setViewValue(transformedValue);
ngModelCtrl.$render();
}
return parseFloat(transformedValue);
}
return undefined;
}
ngModelCtrl.$parsers.push(inputValue);
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment