Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 将modelValue设置为与Angular指令中的viewValue不同的日期格式_Angularjs - Fatal编程技术网

Angularjs 将modelValue设置为与Angular指令中的viewValue不同的日期格式

Angularjs 将modelValue设置为与Angular指令中的viewValue不同的日期格式,angularjs,Angularjs,我在表单中有一个日期选择器。我想为视图设置一种格式,但对模型值(发送到API)应用不同的格式。 简单来说,我希望用户看到'dd/mm/yyyy',但需要以ISO格式发送日期 这是我的指示: app.directive('standardDatepicker', function($timeout) { return{ restrict: 'A', require : '^ngModel', link: function(scope, element, attrs, n

我在表单中有一个日期选择器。我想为视图设置一种格式,但对模型值(发送到API)应用不同的格式。 简单来说,我希望用户看到'dd/mm/yyyy',但需要以ISO格式发送日期

这是我的指示:

app.directive('standardDatepicker', function($timeout) {
return{
    restrict: 'A',  
    require : '^ngModel',
    link: function(scope, element, attrs, ngModel, ngModelCtrl){
            element.datepicker({
                format: "dd/mm/yyyy",
                autoclose: true,
            }).on('changeDate', function(e) {
                ngModel.$viewValue = e.date;    
                ngModel.$render();
            });
    }
  }
});
有没有一种简单的方法可以实现这一点?

您应该使用


有$parser和$formatters来转换值。
有关更多信息,请访问

我最终使用了$parser,如下所示。感谢所有的贡献

app.directive('standardDatepicker', function($timeout, dateFilter) {
return{
    restrict: 'A',  
    require : 'ngModel',
    link: function(scope, element, attrs, ngModel, ngModelCtrl){
            element.datepicker({
                format: "dd/mm/yyyy",
                autoclose: true,
            }).on('changeDate', function(e) {
                ngModel.$parsers.push(function(viewValue){
                    var isoDate = moment(e.date).format('YYYY-MM-DD');
                    return isoDate;
                });
            });
       }
    }
});

你试过使用隐藏的字段吗?我确实考虑过了,但是当你在NG重复中有了它们的时候开始变得复杂起来。想看看是否有一个简单的指令解决方案。
app.directive('standardDatepicker', function($timeout, dateFilter) {
return{
    restrict: 'A',  
    require : 'ngModel',
    link: function(scope, element, attrs, ngModel, ngModelCtrl){
            element.datepicker({
                format: "dd/mm/yyyy",
                autoclose: true,
            }).on('changeDate', function(e) {
                ngModel.$parsers.push(function(viewValue){
                    var isoDate = moment(e.date).format('YYYY-MM-DD');
                    return isoDate;
                });
            });
       }
    }
});