angularjs - 如何在 md-datepicker 中使用 ngMask

标签 angularjs angular-material masking maskedinput

我正在尝试使用 ngMaskmaterial datepicker并且不工作。

任何人都知道如何让它一起工作吗?

一些 ngMask 示例:
http://candreoliveira.github.io/bower_components/angular-mask/examples/index.html#/

最佳答案

这样的事情怎么样:

(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;
    });

    }
})();

并使用这样的指令
<md-datepicker ng-model="myAwesomeModel" os-mask="3?9/19/9999"></md-datepicker>

关于angularjs - 如何在 md-datepicker 中使用 ngMask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40414533/

相关文章:

css - AngularJS orderBy 搞砸了物化 strip 类

angular - <mat-error> 从父组件中的自定义 formControl 继承验证时,在 ControlValueAccessor 中不起作用

html - 使用 CSS 屏蔽 div

css - 为 html 创建透明 mask

javascript - AngularJS 1.x : perform code while in data-ng-repeat. ..?

javascript - 来自 Firebase 的 Angular 显示列表

AngularJS 指令检测在模型属性更改中传递

javascript - 在javascript中从对象数组中删除一个对象

angular-material - 设置角度分量默认值

c# - 密码屏蔽控制台应用程序