How to send createdDt, updatedDat values in the angular form without showing them in template?

Viewed 18

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")
    })
    }
1 Answers

Both createdDt and updateDt should be also FormControls, 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>
Related