【问题标题】:angular model should be text, but input type should be time角度模型应该是文本,但输入类型应该是时间
【发布时间】:2015-08-21 09:36:13
【问题描述】:

我有一个模型,它的时间值为text,如10:00:00,表示10:00 AM。现在我需要能够让用户更改这些值,但它不应该只是input type="text",因为这样很难在 IOS 上更改user 的时间。并且使用type="time" 不起作用,因为模型需要是一个日期对象。

有什么想法可以让用户友好并保持我愚蠢的模型格式吗?

【问题讨论】:

标签: javascript angularjs time getter-setter


【解决方案1】:

这是正确的提示,非常感谢。 这是其他人的解决方案..

mainModule.directive('stupidTimeConverter', function() {

var directive = {};
directive.restrict = 'A';
directive.require = 'ngModel';

directive.link = function(scope, element, attrs, ngModel) {

    // Formatters change how model values will appear in the view
    ngModel.$formatters.push(function(value) {
        return moment(value, "HH:mm:ss").toDate();
    });

    // Parsers change how view values will be saved in the model
    ngModel.$parsers.push(function(value) {
        return moment(value).format("HH:mm:ss");
    });

};
return directive;
});

【讨论】:

    猜你喜欢
    • 2021-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多