【问题标题】:Angular Material - DatepickerAngular 材质 - 日期选择器
【发布时间】:2016-05-16 10:13:38
【问题描述】:

任何人都可以根据官方示例解释为什么 Datepicker 期望值是日期对象吗? https://material.angularjs.org/latest/demo/datepicker

说实话,这很痛苦,因为在将服务器响应绑定到表单之前,我必须确定字段是否为数据类型并转换值:

$scope.myDate = new Date('2015-01-11');

有什么方法可以简单地用字符串值填充日期选择器?

$scope.myDate = '2015-01-11'; 

【问题讨论】:

  • 对不起,如果我的问题不清楚。我的意思是标准的 HTML 日期选择器接受标准化的日期格式 YYYY-MM-DD,例如<input type="date" name="bday" max="1979-12-31" value="1954-11-13"> 直接从服务器响应绑定到 JSON/AJAX 值非常容易。

标签: angularjs-material


【解决方案1】:

字符串值的问题在于解析。 2016 年 5 月 10 日和 2016 年 10 月 5 日可能会混淆。 2016 年 5 月 10 日或 2016 年 10 月 5 日。日期对象可以防止这种情况。你不能使用定义的过滤器将你的字符串数据转换为日期对象吗?

我从 Angular 1.x 的日期过滤器中快速修改了下面的一些代码,它使用 YYYYMMDD (20160516) 的数字日期格式。

/**
 * @name yourDate
 * @ngdoc filter
 * @requires $filter
 * @param DateValue {string} Date Value (YYYY-MM-DD)
 * @returns Date Filter with the Date Object
 * @description
 * Convert date from the format YYYY-MM-DD to the proper date object for future use by other objects/filters
 */
angular.module('myApp').filter('yourDate', function($filter)    {
        var DateFilter = $filter('date');
        return function(DateValue) {
            var Input = "";
            var ResultData = DateValue;
            if ( ! ( (DateValue === null) || ( typeof DateValue == 'undefined') ) ) {
                if ( Input.length == 10)    {
                    var Year = parseInt(Input.substr(0,4));
                    var Month = parseInt(Input.substr(5,2)) - 1;
                    var Day = parseInt(Input.substr(8, 2));
                    var DateObject = new Date(Year, Month, Day);

                    ResultData = DateFilter(DateObject);            // Return Input to the original filter (date)
                } else {
                }
            } else {
            }
            return ResultData;
        };
    }
);

/**
 * @description
 * Work with dates to convert from and to the YYYY-MM-DD format that is stored in   the system.
 */
angular.module('myApp').directive('yourDate',
    function($filter) {
        return {
            restrict: 'A',
            require: '^ngModel',
            link: function($scope, element, attrs, ngModelControl) {
                var slsDateFilter = $filter('yourDate');

                ngModelControl.$formatters.push(function(value) {
                    return slsDateFilter(value);
                });

                ngModelControl.$parsers.push(function(value) {
                    var DateObject = new Date(value);                   // Convert from Date to YYYY-MM-DD
                    return DateObject.getFullYear().toString() + '-' + DateObject.getMonth().toString() + '-' + DateObject.getDate().toString();
                });
            }
        };
    }
);

此代码仅使用标准 Angular Filter 选项,因此您应该能够将其与您的 Material 日期选择器结合使用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-01
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多