I have a create message form and sending a few formcontrols and I need to send the createdDt and updated it as a hidden value where it shouldn't show in the angular template. I want to set the current date and time for createdDt and updatedDt
ngOnInit() : void {
this.addMessageForm : this.formBuilder.group({
'messageTitle' : new FormControl(''),
'messageDec' : new FormControl(''),
'messageSentBy' : new FormControl(''),
'createdDt' : ?
'updateDt' : ?
})
createMessage(this.addMessageForm.value).subscribe(data=> {
console.log("Message Created Successfully")
} ,err => {
console.log("Message Failed to Create")
})
}
CodePudding user response:
Both createdDt
and updateDt
should be also FormControl
s, just don't add them in the form, and if you need to update those properties, do it as usual using setValue
, patchValue
, it doesn't matter since those 2 are not attached to your form
this.addMessageForm : this.formBuilder.group({
'messageTitle' : new FormControl(''),
'messageDec' : new FormControl(''),
'messageSentBy' : new FormControl(''),
'createdDt' : new FormControl('')
'updateDt' : new FormControl('')
})
<!-- no createDt and updateDt -->
<form [formGroup]="addMessageForm">
<input formControlName="messageTitle" />
<input formControlName="messageDec" />
<input formControlName="messageSentBy" />
</form>