AngularMaterial日期选择器忽略了输入的格式。
创始人
2024-10-28 10:01:17
0

在HTML模板中添加一个input元素,通过[(ngModel)]指令将选择的日期值绑定到一个日期对象属性。


  
  
  

在组件类中声明选定日期对象的属性,并使用指令@ViewChild来获取MatDatePicker对象。然后,可以在日期选择器的日期更改事件中更新另一个属性,以便在进行提交前在所需的格式中提供字符串表示形式。

import { Component, ViewChild } from '@angular/core';
import { MatDatepicker } from '@angular/material/datepicker';

@Component({
  selector: 'datepicker',
  templateUrl: './datepicker.component.html',
  styleUrls: ['./datepicker.component.css']
})
export class DatepickerComponent {
  @ViewChild(MatDatepicker) picker: MatDatepicker;
  selectedDate: Date;
  formattedDate: string;

  onDateChange(event): void {
    this.formattedDate = this.selectedDate.toISOString().slice(0, 10);
  }
}

最后,使用相应的格式将选择的日期值提交到服务器。

onSubmit(): void {
  const data = { date: this.formattedDate };
  this.http.post('/api/date', data).subscribe(response => console.log(response));
}

相关内容

热门资讯

透视肯定!aapoker辅助器... 透视肯定!aapoker辅助器怎么用(透视)ai插件(好像存在有挂);所有人都在同一条线上,像星星一...
透视软件!德州真人透视脚本(透... 透视软件!德州真人透视脚本(透视)可靠技巧(2021已更新)(哔哩哔哩)1、游戏颠覆性的策略玩法,独...
透视最新!aapoker插件(... 透视最新!aapoker插件(透视)万能辅助器(总是是真的有挂)在进入aapoker插件辅助挂后,参...
透视软件!werplan有挂吗... 透视软件!werplan有挂吗(透视)2025新版(2026已更新)(哔哩哔哩)1、实时werpla...
透视透视!aapoker怎么提... 透视透视!aapoker怎么提高中牌率(透视)插件下载(都是真的有挂);1、在aapoker怎么提高...
透视了解!拱趴大菠萝机器人(透... 透视了解!拱趴大菠萝机器人(透视)透明挂教程(2025已更新)(哔哩哔哩)1、拱趴大菠萝机器人透视辅...
透视教学!aapoker透视脚... 透视教学!aapoker透视脚本下载(透视)透视脚本下载(其实是有挂)1、任何aapoker透视脚本...
透视软件!pokemmo内置修... 透视软件!pokemmo内置修改器(透视)曝光教程(2022已更新)(哔哩哔哩)1、每一步都需要思考...
透视总结!aapoker怎么选... 透视总结!aapoker怎么选牌(透视)透视脚本下载(一贯是有挂);1、任何aapoker怎么选牌a...
透视智能ai!智星菠萝辅助(透... 透视智能ai!智星菠萝辅助(透视)攻略教程(2024已更新)(哔哩哔哩);1、起透看视 智星菠萝辅助...