项目中要用到格式化金额输入框,要求每三个数字用逗号分割开。
添加一个directive
1 angular.module('myApp.directives', []) 2 .directive('filterInput',['$filter', function ($filter){ 3 return { 4 restrict: 'A', 5 require: 'ngModel', 6 link: function(scope, element, attr, ngModel) { 7 8 ngModel.$parsers.push(function fromUser(text) { 9 return parseInt(text.replace(",","")); 10 }); 11 12 ngModel.$formatters.push(function toUser(text) { 13 console.log($filter('number')(text)); 14 return ($filter('number')(text || '')); 15 }); 16 17 } 18 }; 19 }]);
http://stackoverflow.com/questions/19890364/format-input-value-in-angularjs