簡體   English   中英

如何從角度材料日期選擇器更改日期?

[英]How to change the date from angular material datepicker?

我有一個角度材料的日期選擇器。 單擊某個事件時,我會從 api 服務獲取新日期。 如何更改某些點擊事件的日期?

默認情況下,我有一些約會。 單擊提交按鈕后,有一些 api 處理(假設),在訂閱響應值時有一個日期格式的變量。

如何更改某些點擊事件的日期?

<mat-form-field>
  <input matInput [matDatepicker]="picker" placeholder="Choose a date">
  <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
  <mat-datepicker #picker></mat-datepicker>
</mat-form-field> <br/>
<button (click)="dosomeaction()">Submit</button>


export class DatepickerOverviewExample {

   dosomeaction = function(){
     let date = "2018/09/04"; 
     //assign the above date to the input field
   } 
}

閃電戰

你可以這樣做

組件.html

<mat-form-field>
  <input matInput [matDatepicker]="picker" 
  [(ngModel)]="dateToPass"placeholder="Choose a date">
   <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
   <mat-datepicker #picker></mat-datepicker>
   </mat-form-field> <br/>
  <button (click)="dosomeaction()">Submit</button>

組件.ts

  export class DatepickerOverviewExample {

  dateToPass;

 dosomeaction(){
   let date = new Date("2018/09/04");
   //assign the above date to the input field
   this.dateToPass = date;

  }
}

看看這個Stackblitz ,只需在ngModel設置日期

export class DatepickerOverviewExample {
   start_time: Date;
   dosomeaction = function(){
      start_time: new Date();
   } 
}

 <input mdInput
     name="start_time"
     #start_time="ngModel"
     date="true"
     [(ngModel)]="start_time"
     placeholder="Choose a date">

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM