简体   繁体   English

Angular TypeScript 如何分配日期

[英]Angular TypeScript how to assign Date

How can i bind date to date selector?如何将日期绑定到日期选择器?

const TodayDate = "19-11-2020";
ngOnInit() {
  this._MyregisterForm = this.formBuilder.group({
    today_Date:[this.TodayDate, [Validators.required]]
  });
}

HTML HTML

<form [formGroup]="_MyregisterForm" (ngSubmit)="onSubmit()">
  <input type="date" formControlName="today_Date" value="{{TodayDate}}">
</form>

Remove value="{{TodayDate }}"您正在绑定一个具有 TodayDate 值的控件,这就足够了。

I would try to keep the dates as javascript dates in your app.我会尝试将日期保留为您的应用程序中的 javascript 日期。 It will make it easier to format them differently and use them with angular materials date picker.这将使它们更容易以不同的方式格式化并将它们与角材料日期选择器一起使用。

use the library date-fns to parse the string into a date.使用库 date-fns 将字符串解析为日期。

https://date-fns.org/v2.16.1/docs/parse https://date-fns.org/v2.16.1/docs/parse

Then pass that date into the formBuilder.然后将该日期传递给 formBuilder。

No need to put value attribute in input when are using formControlName使用formControlName时无需将value属性放入输入中

Stackblitz Demo Stackblitz 演示

component.html组件.html

<form [formGroup]="_MyregisterForm" (ngSubmit)="onSubmit()">
    <input type="date" formControlName="today_Date">
    <button type="submit">Submit</button>
</form>

component.ts组件.ts

import { Component, VERSION } from "@angular/core";
import { FormBuilder, FormGroup, Validators } from "@angular/forms";
import { DatePipe } from "@angular/common";

@Component({
  selector: "my-app",
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.css"],
  providers: [DatePipe]
})
export class AppComponent {
  _MyregisterForm: FormGroup;
  TodayDate = "19-11-2020";

  constructor(private formBuilder: FormBuilder, private datePipe: DatePipe) {
    this._MyregisterForm = this.formBuilder.group({
      today_Date: [this.getTransformedDate(this.TodayDate), Validators.required]
    });
  }

  private getTransformedDate(date) {
    let date1 = date.split("-");
    let newDate = date1[2] + "-" + date1[1] + "-" + date1[0];
    return newDate;
  }

  onSubmit() {
    const date = this.datePipe.transform(
      this._MyregisterForm.get("today_Date").value,
      "dd-MM-yyyy"
    );
    alert(date);
  }
}

I provide a Stackblitz just here .在这里提供了一个 Stackblitz。

The date format is not good, you need to use International format.日期格式不好,需要使用国际格式。 Moreover, like everyone said, you don't need to bind a value .而且,就像大家说的,你不需要绑定一个value

_MyregisterForm: FormGroup;
TodayDate = "2020-12-10";

constructor(private formBuilder: FormBuilder) {
  this._MyregisterForm = this.formBuilder.group({
    today_Date: [this.TodayDate, Validators.required]
  });
}
<form [formGroup]="_MyregisterForm">
    <input type="date" formControlName="today_Date">
</form>
<pre>{{ _MyregisterForm.value | json }}</pre>

TodayDate should be; TodayDate应该是;

TodayDate = new Date();

and remove value attribute from input并从input删除value属性

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM