角度材料日期验证未按预期进行

Mah*_*van 5 angular-material angular angular8 mat-datepicker

我正在研究mat-datepicker用户可以输入值并使用我的代码选择日期的位置它工作正常。我有一个更新按钮,默认情况下它将处于禁用模式。当用户满足两个条件时,只有更新按钮才会启用。

这是我的屏幕截图,它看起来像

在此输入图像描述

如果我清除最后日期,则一旦用户输入最后日期,更新按钮将处于禁用模式,那么只有更新将在此处启用我遇到了问题

这是我的 ts 代码

dateValidator(input) {
    //console.log(input);
    console.log(Object.prototype.toString.call(input) === '[object Date]', !this.disableDate(), input.length > 0)
    if (!!input) { // null check
        if (Object.prototype.toString.call(input) === '[object Date]' && !this.disableDate() && input.length > 0) {
            this.disableUpdateBtn = false;
            console.log("Date is Valid!!");
        } else {
        this.disableUpdateBtn = true;
        console.log("Date is Invalid!!");
        }
    } else {
        this.disableUpdateBtn = true;
        console.log("Date is Invalid!!");
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的HTML“更新”按钮的代码

<button mat-flat-button color="primary" (click)="updateMilestone();" cdkFocusInitial [disabled]="disableUpdateBtn">
    Update
</button>
Run Code Online (Sandbox Code Playgroud)

当我单击清除按钮,然后开始输入日期,然后输入 时,出现错误01。我没有收到任何错误,但是当我开始输入Dec.

在此输入图像描述

我正在检查 if 条件中的输入是否为 null 为什么仍然无法读取 null 属性

Jua*_*ero 2

尝试将其更改input.length为:input?.length在您的代码中(所有实例)