简体   繁体   English

如何正确使用 <input type=“date”> 在角?

[英]How to correctly use <input type=“date”> in Angular?

I have an input control as follows: 我有一个输入控件,如下所示:

<input type="date" (change)="getValues($event)" [ngModel]="customer.dob| date:'yyyy-MM-dd'" (ngModelChange)="customer.dob = $event">

When I use keyboard to type the date it resets to dd/MM/yyyy . 当我使用键盘输入日期时,它将重置为dd/MM/yyyy Mouse works fine. 鼠标工作正常。 Any suggestions? 有什么建议么?

There is a whole article about it in Angular 2: How to use date input controls with Angular Forms . Angular 2中有整篇文章:如何在Angular Forms中使用日期输入控件

The solution is to implement a new value accessor for date input controls: 解决方案是为日期输入控件实现一个新的值访问器:

// date-value-accessor.ts

import { Directive, ElementRef, HostListener, Renderer, forwardRef } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';

export const DATE_VALUE_ACCESSOR: any = {
  provide: NG_VALUE_ACCESSOR,
  useExisting: forwardRef(() => DateValueAccessor),
  multi: true
};

/**
* The accessor for writing a value and listening to changes on a date input element
*
*  ### Example
*  `<input name="myBirthday" type="date" />`
*/
@Directive({
  selector: '[useValueAsDate]',
  providers: [DATE_VALUE_ACCESSOR]
})
export class DateValueAccessor implements ControlValueAccessor {

  @HostListener('input', ['$event.target.valueAsDate']) onChange = (_: any) => { };
  @HostListener('blur', []) onTouched = () => { };

  constructor(private _renderer: Renderer, private _elementRef: ElementRef) { }

  writeValue(value: Date): void {
    this._renderer.setElementProperty(this._elementRef.nativeElement, 'valueAsDate', value);
  }

  registerOnChange(fn: (_: any) => void): void { this.onChange = fn; }
  registerOnTouched(fn: () => void): void { this.onTouched = fn; }

  setDisabledState(isDisabled: boolean): void {
    this._renderer.setElementProperty(this._elementRef.nativeElement, 'disabled', isDisabled);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用日期选择器作为表单的输入 - angular - How to use a date picker as an input in a form - angular 如何在我的angular4应用程序中使用输入类型日期? 当前浏览器是mozilla firefox - how to use input type date in my angular4 app? current browser is mozilla firefox 如何在 HTML 中获取输入类型 Date 并在 angular7 component.ts 文件中使用? - How to take input type Date in HTML and use in angular7 component.ts file? 角度输入类型日期:如何获取当前日期预设? - Angular input type date: how to get the current date preset? 如何在 angular 4 中更改 input type=&quot;date&quot; 的输出日期格式? - How to change the output date format of input type=“date” in angular 4? 如何显示日期<input type="date">使用 Angular 时的字段 - How to display a date in <input type = "date"> field when using Angular Angular 4 - 如何在输入类型中使用货币管道 - Angular 4 - How to use currency pipe in input type 如何在 angular 材料中使用输入类型文件 - How to use input type file in angular material Angular 4输入类型日期格式 - Angular 4 input type date format 输入类型 = 数字验证在 angular 中无法正常工作 - Input type=number validation is not working correctly in angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM