angular input 去空格

 
方案一:ng-blur事件,正则去空格
<input type="tel" id="mobileInpt" 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" id="mobileInpt" 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, '');
     });
   }
 }
}])

 

 
posted @ 2017-02-07 14:57  MiyaMiya  阅读(1017)  评论(0编辑  收藏  举报