Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html ng-app="app">
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
<input type="text" remove-char="s" ng-model="val" />
<p>Var: {{val}}</p>
  
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
</body>
</html>
 
angular
  .module('app', [])
  .directive('removeChar', removeCharDirective)
;
function removeCharDirective() {
  
  return {
    require: 'ngModel',
    link: function (scope, el, attr, ctrl) {
      
      var expression = new RegExp(
        '[' + attr.removeChar + ']+', 'g'
      );
      
      ctrl.$parsers.unshift(function (val) {
        if (!angular.isString(val)) {
          val = val ? val.toString() : '';
        }
        
        if (expression.test(val)) {
          val = val.replace(expression, '');
          ctrl.$setViewValue(val);
          ctrl.$render();
        }
        
        
        return val;
      });
    }
  };
}
Output

This bin was created anonymously and its free preview time has expired (learn why). — Get a free unrestricted account

Dismiss x
public
Bin info
anonymouspro
0viewers