gpt4 book ai didi

Angular matDatepicker 区域设置无法在输入框中输入内容

转载 作者:行者123 更新时间:2023-12-01 23:11:23 26 4
gpt4 key购买 nike

如果我使用日期选择器,如

<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>

并设置区域设置,如

  providers: [    
{ provide: LOCALE_ID, useValue: 'it-IT' },
{ provide: MAT_DATE_LOCALE, useValue: 'it-IT' }]

如果用户从选择器中选择日期,它会起作用,但如果他在输入框中键入,则日期不会以正确的方式解析。例如:意大利语格式为 dd/mm/yyyy 如果用户在框中选择 2018 年 3 月 31 日,我们会得到“31/03/2018”(没问题),但如果他输入“31/03/2018”,则日期不是验证(但它是有效的意大利日期)。如果他输入“03/05/2018”,我们会得到 3 月 5 日,而不是 5 月 3 日。这是一个 Angular Material 错误还是我犯了一些错误?

我还尝试用“it”代替“it-IT”。

最佳答案

根据 Angular Material 示例,您可以使用此

import {Component} from '@angular/core';
import {MAT_MOMENT_DATE_FORMATS, MomentDateAdapter} from '@angular/material-moment-adapter';
import {DateAdapter, MAT_DATE_FORMATS, MAT_DATE_LOCALE} from '@angular/material/core';

/** @title Datepicker with different locale */
@Component({
selector: 'datepicker-locale-example',
templateUrl: 'datepicker-locale-example.html',
styleUrls: ['datepicker-locale-example.css'],
providers: [
// The locale would typically be provided on the root module of your application. We do it at
// the component level here, due to limitations of our example generation script.
{provide: MAT_DATE_LOCALE, useValue: 'ja-JP'},

// `MomentDateAdapter` and `MAT_MOMENT_DATE_FORMATS` can be automatically provided by importing
// `MatMomentDateModule` in your applications root module. We provide it at the component level
// here, due to limitations of our example generation script.
{provide: DateAdapter, useClass: MomentDateAdapter, deps: [MAT_DATE_LOCALE]},
{provide: MAT_DATE_FORMATS, useValue: MAT_MOMENT_DATE_FORMATS},
],
})

确保npm install --save package @angular/material-moment-adapteryarn add package @angular/material-moment-adapter

关于Angular matDatepicker 区域设置无法在输入框中输入内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52818180/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com