jQuery Mask Plugin Integration with Angular.js

Simple example showing how to integrate jQuery Mask Plugin with Angular.js

by Frédérick Deslandes

HTML

<script src="https://igorescobar.github.io/jQuery-Mask-Plugin/js/jquery.mask.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular.min.js"></script>
<div ng-app="myApp">
  <h1>jQuery Mask Plugin integration with Angular.js</h1>
  <h3>Simple example</h3>
  <br />
  <div ng-controller="formController as form">
    <label>Date: </label>
    <br />
    <input ng-model="form.someValue" id="date" ui-date>
    <input ng-model="form.someValue">
  </div>
</div>

CSS

body {
  margin: 50px;
}

h1 {
  font-size: 26px;
  margin-bottom: 10px;
}

JavaScript

var myApp = angular.module('myApp', []);

myApp.directive('uiDate', function() {
  return {
    require: 'ngModel',
    link: function($scope, element, attrs, ngModelCtrl) {
      element.mask("00/00/0000");
      ngModelCtrl.$parsers.unshift(function(value) {
        return element.cleanVal();
      });
      ngModelCtrl.$formatters.unshift(function(value) {
        return element.masked(value);
      });
    }
  };
});

myApp.controller('formController', function FormController() {
  this.someValue = '01032017';
});