使用时无需value
在输入中输入属性formControlName
Stackblitz 演示
组件.html
<form [formGroup]="_MyregisterForm" (ngSubmit)="onSubmit()">
<input type="date" formControlName="today_Date">
<button type="submit">Submit</button>
</form>
组件.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);
}
}