简体   繁体   English

ui-bootstrap timepicker和ng-model值转换器

[英]ui-bootstrap timepicker and ng-model value converter

my server side json uses "HH:mm:ss" to store time data. 我的服务器端json使用“HH:mm:ss”来存储时间数据。 i'm using UI-Bootsrap TimePicker to let the user input the time. 我正在使用UI-Bootsrap TimePicker让用户输入时间。

I'm using the ngModelController's $parsers and $formatters to map from server side json to frontend timepickerUI. 我正在使用ngModelController的$ parsers和$ formatters从服务器端json映射到frontend timepickerUI。 The conversion from UI to Data is working as expected, however i'm having difficulties converting the Data back to UI. 从UI到Data的转换正如预期的那样工作,但是我很难将数据转换回UI。

The error message is as follows: 'Timepicker directive: "ng-model" value must be a Date object, a number of milliseconds since 01.01.1970 or a string representing an RFC2822 or ISO 8601 date.' 错误消息如下:'Timepicker指令:“ng-model”值必须是Date对象,自01.01.1970以来的毫秒数或表示RFC2822或ISO 8601日期的字符串。

this is the HTML markup 这是HTML标记

<timepicker ng-model="HotelProperties.CheckOutTime" time-span-converter hour-step="hstep" minute-step="mstep" show-meridian="false"></timepicker>

this is the directive 这是指令

.directive("timeSpanConverter", ['$filter',function($filter) {
        return {
            restrict: 'A',
            require: 'ngModel',
            link: function(scope, element, attr, ngModelCtrl) {
                function fromJson(value) {

                    if (!value) return new Date();

                    var parts = value.split(':');
                    var hh = Number(parts[0]);
                    var mm = Number(parts[1]);

                    var date = new Date();
                    date.setHours(hh);
                    date.setMinutes(mm);

                    return date;
                }

                function fromUser(value) {
                    var time = $filter('date')(value, "HH:mm:00");
                    return time;
                }

                ngModelCtrl.$parsers.push(fromUser);
                ngModelCtrl.$formatters.push(fromJson);
            }

        }
    }])

The Timepicker is expecting you to pass in a Date object, not a string. Timepicker期望您传入Date对象,而不是字符串。

Quick help:- 快速帮助: -

Your hours :minutes are String so it raise such an error i just figured how to get rid of it . 你的小时:分钟是字符串所以它引发了这样的错误,我只是想办法摆脱它。 Simple Go ahead and Install Moment js and format like the Following $scope.timing= moment('09:30 AM','hh:mm A').format(); 简单继续并安装Moment js和格式,如以下$ scope.timing = moment('09:30 AM','hh:mm A')。format();

Little Prolonged Explanation :- From the Timepicker documentation You may Need to Use Timepicker (ui.bootstrap.timepicker) from Angular UI. Little Prolranded说明: - 从Timepicker 文档中,您可能需要使用Angular UI中的Timepicker(ui.bootstrap.timepicker)。

ng-model: The Date object that provides the time state. ng-model:提供时间状态的Date对象。

I'm using MomentJS to format my date from a string to a Date object. 我正在使用MomentJS将日期从字符串格式化为Date对象。

See the MomentJS parse documentation 请参阅MomentJS解析文档

You can transform your times back to a string if required using the same library. 如果需要,您可以使用相同的库将时间转换回字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM