Angular 角度获取错误中的Mat日期选择器自定义值

Angular 角度获取错误中的Mat日期选择器自定义值,angular,angular8,mat,mat-datepicker,Angular,Angular8,Mat,Mat Datepicker,我正在处理Mat date picker自定义值。这是我正在尝试的代码 <div class="form-group" *ngIf="selectVal.value == 'Date'"> <mat-form-field> <input matInput [matDatepicker]="picker" (dateChange)="formatDate(dateValue.value)" #

我正在处理Mat date picker自定义值。这是我正在尝试的代码

      <div class="form-group" *ngIf="selectVal.value == 'Date'">
                <mat-form-field>
                <input matInput [matDatepicker]="picker" (dateChange)="formatDate(dateValue.value)" #dateValue  formControlName="assumptionValueInDatetime" class="form-control" placeholder="Choose a date" >
                    <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
                    <mat-datepicker #picker></mat-datepicker>
                </mat-form-field>
            </div>
这就是我的有效载荷的样子:

    initassumpationForm() {
        this.assumptionDesignForm = this.fb.group({
            assumptionValueInDatetime: this.formatDate(this.assumptionValueInDatetime)
        });     
    }
将错误获取为:

错误类型错误:无法读取未定义的属性“getDate”


最好的方法是构建一个自定义日期适配器(这是一项服务)。当您使用JavaScript
Date
对象作为基础日期格式时,您只需扩展
@angular/material/core
中提供的
NativeDateAdapter
,并覆盖两种方法:
parse
format
。前者从字符串格式中获取一个
Date
对象(例如:01-MAR-2020),后者从
Date
对象中获取一个字符串对象(如问题文本中提供的
formatDate
函数)

构建自定义本机日期适配器类后,只需使用令牌
DateAdapter
AppModule
中提供它:

从'@angular/material/core'导入{DateAdapter};
...
@NGD模块({
...
提供程序:[{提供:日期适配器,使用类:CustomNativeDateAdapter}]
...
})
导出类AppModule{}
关于
CustomNativeDateAdapter
类,在您的例子中,它将类似于:

从'@angular/core'导入{Injectable};
从“@angular/material/core”导入{NativeDateAdapter};
@可注射()
导出类CustomNativeDateAdapter扩展了NativeDateAdapter{
月份:字符串[]=['一月'、'二月'、'三月'、'四月'、'五月',
“六月”、“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”];
monthsObj:{[key:string]:number}={JAN:0,FEB:1,MAR:2,APR:3,
五月四日、六月五日、七月六日、八月七日、九月八日、十月九日、十一月十日、十二月十一日;
解析(值:任意):日期|空{
如果(值的类型=='number'){
返回新日期(值);
}
常量dateRegex:RegExp=/^(0 | 1)[1-9]-[A-Z]{3}-(1 | 2)[0-9]{3}$/;
if(typeof value=='string'&&dateRegex.test(value)){
const dateSplit=value.split('-');
返回新日期(+dateSplit[2],本月目标[dateSplit[1]],
+dateSplit[0],12,0,0,0);
}
返回null;
}
格式(日期:日期,显示格式:对象):字符串{
如果(!this.isValid(日期)){
抛出错误('NativeDateAdapter:无法设置无效日期的格式');
}
让formatDate=('0'+date.getDate()).slice(-2)+
'-'+this.months[date.getMonth()]+'-'+date.getFullYear();
返回日期;
}
}

试试这个:新日期(this.assumptionValueInDatetime)@Chellappanவ 如果我使用这种格式,它将如何转换为2020年6月12日的格式idea@Chellappanவ 是的,你检查过这个了吗
    initassumpationForm() {
        this.assumptionDesignForm = this.fb.group({
            assumptionValueInDatetime: this.formatDate(this.assumptionValueInDatetime)
        });     
    }