angular input 去空格
方案一:ng-blur事件,正则去空格
<input type="tel" class="f14" name="mobile" value="" placeholder="请输入您的手机号" ng-model="vm.mobile" ng-blur="vm.mobileRE($event)" required>
vm.mobileRE = function($event) { var that = $event.currentTarget; $(that).val(function(n, c) { return c.replace(/(^\s*)|(\s*$)/g, ""); }); }
方案二:指令,keyup事件
<input type="tel" class="f14" name="mobile" value="" placeholder="请输入您的手机号" ng-model="vm.mobile" space-filter="mobileRE" required>
angular.module('app').directive('spaceFilter', [function() { return { require: "ngModel", link: function(scope, element, attrs, ngModel) { var attr = attrs.spaceFilter; if (attr) { var dataType = { "mobileRE": /\s*/g } var regex = dataType[attr]; } element.bind('keyup', function(value) { this.value = this.value.replace(regex, ''); }); } }
}])
参考地址: https://www.zhihu.com/question/39843323
http://www.cnblogs.com/whitewolf/p/angular-input-box-format.html