angular - 如何从 Angular Material 日期选择器更改日期?

标签 angular typescript angular6

我有一个来自 Angular Material 的日期选择器。单击某个事件时,我从 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
   } 
}

stackblitz

最佳答案

你可以这样做

组件.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;

  }
}

关于angular - 如何从 Angular Material 日期选择器更改日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52100345/

相关文章:

angular - 如何使用 Typescript 源代码调试/步入 npm 包?

angular - 使用来自 http ://ng2-uploader. com 的 Angular2 文件 uploader 时出错

angular - 如果 Angular2 出现错误,服务器应该返回什么样的 JSON

angular - 我在 angular 2 cli 中的 webpack 配置文件在哪里?

javascript - 健康检查网址不起作用 : Cannot match any routes

javascript - TypeScript 类型 'object' 与 Object 接口(interface),为什么没有 'array' 类型?

html - Angular 4将值从html模板传递给组件中的方法

angular - Karma ng 测试未在控制台中显示构建错误

css - ngStyle 被应用了多少次

html - Angular 6 routerLinkActive 不工作