4

I am trying to use ngMask with material datepicker and is not working.

Anyone, has any idea how to put it to work together ?

Some ngMask examples: http://candreoliveira.github.io/bower_components/angular-mask/examples/index.html#/

Thalles Noce
  • 791
  • 1
  • 9
  • 20
  • md-datepicker gets invalid therefore will block the submit if the value does not fit as a valid date, however enforce it with a mask would be good, at least on desktop. you could fill a issue report on https://github.com/angular/material and present them this codepen: http://codepen.io/anon/pen/GNRWQd?editors=1000 Sadly, there seems to have no easy integration – Sombriks Nov 04 '16 at 04:24
  • Thank you @Sombriks for you reply. I followed your instructions and opened a issue. **[Issue #9976](https://github.com/angular/material/issues/9976)** – Thalles Noce Nov 06 '16 at 15:16

1 Answers1

2

How about something like this:

(function () {

    'use strict';

    /**
     * Setup a custom date formatter, I'm using moment for example
     */
    angular
        .module('awesomemodule')
        .constant('DEFAULT_DATE_FORMAT', 'DD/MM/YYYY')
        .config(['DEFAULT_DATE_FORMAT', '$mdDateLocaleProvider', dateConfig]);

    function dateConfig (DEFAULT_DATE_FORMAT, $mdDateLocaleProvider) {

        $mdDateLocaleProvider.formatDate = osDateFormatter;

        function osDateFormatter(date) {
            if (!date) {
                date = new Date();
            }

            return moment(date).format(DEFAULT_DATE_FORMAT);
        }
    }

})()


(function () {
    'use strict';

    /**
     * Decorate the mdDatepickerDirective to add ngMask
     */
    angular
        .module('awesomemodule')
        .config(['$provide', checkForNgMask]);

    function checkForNgMask ($provide) {

        $provide.decorator('mdDatepickerDirective', function ($delegate) {
        var directive = $delegate[0];

        var template = directive.template;

        directive.template = function (tElement, tAttrs) {
            var originalTemplate = template.apply(this, arguments);

            if (R.has('osMask', tAttrs)) {
                var element = angular.element(originalTemplate);
                element.find('input').attr('mask', tAttrs.osMask);
                element.find('input').attr('ng-model', "ctrl.dateInput");//ng-model is required by ngMask
                return R.map(R.prop('outerHTML'), R.values(element)).join("");
            }

            return originalTemplate;
        };

        return $delegate;
    });

    }
})();

And use the directive like this

<md-datepicker ng-model="myAwesomeModel" os-mask="3?9/19/9999"></md-datepicker>
Daniele
  • 1,063
  • 1
  • 10
  • 22
  • what is "R", this variable is not declared – Higarian Mar 23 '17 at 18:28
  • 1
    can you give one solution without Randal? I didnt want to put another library just because i want to put mask on a field. it would help a lot. I tried to replace myself but i got a little confused on watch the return statement is expecting – Higarian Mar 27 '17 at 12:36
  • 1
    https://github.com/angular/material/issues/9976#issuecomment-332529645 This implementation is much better because it works with default date values! – tstancin Sep 29 '17 at 14:15