【问题标题】:How can I set the default date in input portion?如何在输入部分设置默认日期?
【发布时间】:2021-02-28 15:07:07
【问题描述】:
    <mat-form-field>
        <input matInput [matDatepicker]="picker1" placeholder="From date: mm-dd-yyyy" 
        name="from_date" [(ngModel)]="callListRequestOb.from_date" maxlength="150">
        <mat-datepicker-toggle matSuffix [for]="picker1"></mat-datepicker-toggle>
        <mat-datepicker #picker1></mat-datepicker>
    </mat-form-field

(.ts 文件)

    constructor(
            private modalService: NgbModal,
            private aService: ApplicationService,
        ) {
            this.callListRequestOb = new CallListRequestModel();
            this.currentUser = JSON.parse(localStorage.getItem('currentUser'));
            this.callListRequestOb.from_date = moment().format('MM-DD-YYYY');
            this.callListRequestOb.to_date = moment().format('MM-DD-YYYY');
            this.callListRequestOb.page_no = this.page;
            this.callListRequestOb.row_per_page = this.pageSize;
        }

这个模板驱动的“表单”如何在 dom 输入部分设置今天的日期?我必须在这里使用[(ngModel)]。

【问题讨论】:

标签: angular mat-datepicker


【解决方案1】:

请试试这个,

test.component.html

<mat-form-field>
    <input matInput [matDatepicker]="picker1" placeholder="From date: mm-dd-yyyy" 
    name="from_date" [(ngModel)]="callListRequestOb.from_date" maxlength="150">
    <mat-datepicker-toggle matSuffix [for]="picker1"></mat-datepicker-toggle>
    <mat-datepicker #picker1></mat-datepicker>
</mat-form-field>

test.component.ts

callListRequestOb={
    from_date: new Date()
}

【讨论】:

  • 在构造函数中我写了这个:this.callListRequestOb.from_date = moment().format('MM-DD-YYYY'); @托尼
【解决方案2】:

在 CallListModel.ts 文件中,我将 from_date & to_date 类型更改为 Date。

export class CallListRequestModel{
    from_date: Date;
    to_date: Date;
 }

然后在构造函数中,我写了这个并且它正在工作。

this.callListRequestOb.from_date = new Date();

谢谢大家帮助我。

【讨论】:

    猜你喜欢
    • 2022-12-05
    • 1970-01-01
    • 1970-01-01
    • 2020-03-25
    • 1970-01-01
    • 2016-03-10
    • 1970-01-01
    • 1970-01-01
    • 2011-10-22
    相关资源
    最近更新 更多