Angular.js和HTML5日期输入值 - 如何让Firefox在日期输入中显示可读取的日期值? [英] Angular.js and HTML5 date input value -- how to get Firefox to show a readable date value in a date input?

查看:155
本文介绍了Angular.js和HTML5日期输入值 - 如何让Firefox在日期输入中显示可读取的日期值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个HTML5日期输入,我希望它的值默认设置为我的模型中的date属性的值。我不太关于格式化,因为Chrome似乎根据我的区域设置决定了我,但理想的格式是一致的 dd / MM / yyyy 。 p>



目前不支持 type =date的Firefox将将所有值转换为字符串。由于您(正确地)希望 date 成为一个真正的 Date 对象而不是一个字符串,我认为最好的选择是创建另一个变量,例如 dateString ,然后链接两个变量:

 < input type =dateng-model =dateString/> 



  function MainCtrl ,dateFilter){
$ scope.date = new Date();

$ scope。$ watch('date',function(date)
{
$ scope.dateString = dateFilter(date,'yyyy-MM-dd');
});

$ scope。$ watch('dateString',function(dateString)
{
$ scope.date = new Date(dateString);
});
}

小提琴



实际结构仅用于演示目的。您最好创建自己的指令,特别是为了:





请注意,我使用了 yyyy-MM-dd ,因为它是由JavaScript Date直接支持的格式对象。如果您想使用另一个,您必须自己进行转换






编辑



这是一个清除伪指令的方法:

  myModule.directive(
'dateInput',
function(dateFilter){
return {
require:'ngModel',
template:'< input type =date>< / input>',
replace:true,
link:function(scope,elm,attrs,ngModelCtrl){
ngModelCtrl。$ formatters.unshift (modelValue){
return dateFilter(modelValue,'yyyy-MM-dd');
});

ngModelCtrl $ parsers.unshift(function(viewValue){
返回新日期(viewV alue);
});
},
};
});

Fiddle



这是一个基本的指令,还有很大的改进空间,例如:




  • 允许使用自定义格式而不是 yyyy-MM-dd

  • 检查用户输入的日期是否正确。


I have an HTML5 date input and I would like its value to be set to the value of the date property in my model by default. I'm not too fussy about formatting since Chrome seems to decide that for me anyway based on my locale, but ideally the format would be consistently dd/MM/yyyy.

Fiddle

This is how I set up my input:

<input type="date" 
       ng-model="date" 
       value="{{ date | date: 'yyyy-MM-dd' }}" />

This works fine on Chrome, and I see the following by default:

(I still don't quite understand why the value had to be given in yyyy-MM-dd, if Chrome still formats it based on my locale, but that's a different question.)

My issue is with Firefox not showing the date's value in the way I've specified. I think this has to do with binding the input to the date model, because I can specify pretty much any string in the value attribute, and I will still see the long date string in the input by default:

If I remove ng-model="date" from the input tag, Firefox nicely displays any value I give it. I didn't think the model that an input was bound to actually had any effect on its default value?

I understand the date input isn't supported universally, but seeing as it's supposed to fall back on a simple text input, I don't see why its value won't simply be 2013-08-05, as specified by angular's date filter.

So, how do I get Firefox to accept my formatted value in the date input?


NOTE After the edits have been done by the user, I will of course perform validation and convert each date input value into a proper Date object. Not sure if this is relevant to the question, but putting it out there just in case, because the input formats would obviously need to be consistent for the date conversion to work the same in all browsers. Problematic, of course, with Chrome deciding the input format for me...

解决方案

The problem is that value is ignored when ng-model is present.

Firefox, which doesn't currently support type="date", will convert all the values to string. Since you (rightly) want date to be a real Date object and not a string, I think the best choice is to create another variable, for instance dateString, and then link the two variables:

<input type="date" ng-model="dateString" />

function MainCtrl($scope, dateFilter) {
    $scope.date = new Date();

    $scope.$watch('date', function (date)
    {
        $scope.dateString = dateFilter(date, 'yyyy-MM-dd');
    });

    $scope.$watch('dateString', function (dateString)
    {
        $scope.date = new Date(dateString);
    });
}

Fiddle

The actual structure is for demonstration purposes only. You'd be better off creating your own directive, especially in order to:

Please notice that I've used yyyy-MM-dd, because it's a format directly supported by the JavaScript Date object. In case you want to use another one, you must make the conversion yourself.


EDIT

Here is a way to make a clean directive:

myModule.directive(
    'dateInput',
    function(dateFilter) {
        return {
            require: 'ngModel',
            template: '<input type="date"></input>',
            replace: true,
            link: function(scope, elm, attrs, ngModelCtrl) {
                ngModelCtrl.$formatters.unshift(function (modelValue) {
                    return dateFilter(modelValue, 'yyyy-MM-dd');
                });

                ngModelCtrl.$parsers.unshift(function(viewValue) {
                    return new Date(viewValue);
                });
            },
        };
});

Fiddle

That's a basic directive, there's still a lot of room for improvement, for example:

  • allow the use of a custom format instead of yyyy-MM-dd,
  • check that the date typed by the user is correct.

这篇关于Angular.js和HTML5日期输入值 - 如何让Firefox在日期输入中显示可读取的日期值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆