datepicker显示错误

datepicker显示错误

本文介绍了手动输入日期时,md-datepicker显示错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用md-datepicker,当我手动放置日期时,它显示无效的日期,但是如果我从此控件中选择日期,则它会得到验证.为此,我使用了以下代码

I am using md-datepicker, when i put date manually it is showing invalid date but if i select date from this control then it is validated. for this i have used following code

    <md-datepicker name="FromDate" ng-model="vm.user.FromDate" ng-required="true" id="FromDate" md-is-error="login.ToDate.$invalid && login..$submitted" md-placeholder="FromDate"></md-datepicker>
<div ng-messages="login.FromDate.$error" md-auto-hide="false" ng-show="login.FromDate.$touched ||login.FromDate.$submitted">
<div ng-message="required">Please enter FromDate.</div>                    </div>

推荐答案

我找到了答案.在配置部分以及formatDate中,我添加了parseDate函数,并且在手动输入以及从控件中选择时正常工作没有错误

i found the answer. in config section along with formatDate i added parseDate function and it is working fine no error while manual entry as well as selection from control

  .config(function ($mdDateLocaleProvider) {
        $mdDateLocaleProvider.formatDate = function (date) {
            return date ? moment(date).format('DD-MM-YYYY') : '';
        };

        $mdDateLocaleProvider.parseDate = function (dateString) {
            var m = moment(dateString, 'DD-MM-YYYY', true);
            return m.isValid() ? m.toDate() : new Date(NaN);
        };
    });

这篇关于手动输入日期时,md-datepicker显示错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 03:17