I want to display the current date as a default value,is not working if I use the formControlName but I need to use the formControlName.
Here is my HTML file
<ion-item>
<ion-label color="primary"> Start date </ion-label>
<ion-datetime [value]="startDate.toISOString()" formControlName="startDate" required></ion-datetime>
</ion-item>
<div id="startDateErrorMessage" *ngIf="newProjForm.controls['startDate'].invalid &(newProjForm.controls['startDate'].dirty || newProjForm.controls['startDate'].touched)">
<span class="error ion-padding" *ngIf="newProjForm.controls['startDate'].errors.required"> Date is required </span>
</div>
and here is my TS file
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { Component, OnInit } from '@angular/core';
import { AlertController } from '@ionic/angular';
@Component({
selector: 'app-new-project',
templateUrl: './new-project.page.html',
styleUrls: ['./new-project.page.scss'],
})
export class NewProjectPage implements OnInit {
newProjForm: FormGroup;
startDate: Date = new Date();
constructor(private formBuilder: FormBuilder, private alertCtrl: AlertController) { }
ngOnInit() {
this.newProjForm = this.formBuilder.group({
startDate: ['' ,[Validators.required]]
});
}
}
when I compile the project I'm getting this errors
"ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'toISOString' of undefined"
"TypeError: Cannot read property 'toISOString' of undefined"
Can anyone point me in the right direction please?
2条答案
按热度按时间zdwk9cvp1#
要在表单生成器对象中设置初始值,请不要在html模板中使用[value]。您可以将"“替换为字段的初始值,如下所示:
xzv2uavs2#
由于时间显示不正确而出错。您要查找的答案是:
// html:
// ts: