I'm using one angular form to add and edit employee details. valueChanges.subscribe()
helps to detect the change of the respective value through the dropdown when adding a new employee.
this.createEmpForm.get('employee_id').valueChanges.subscribe(value => {
...
});
But in the edit mode, when going pre-populated and saving the data dropdown/input box of a specific user with some new data, a backend error occurs saying some fields are empty. The reason is that even if a data is set to the dropdown through the backend in edit mode, the valueChanges.subscribe()
will not fire with relevant data because the dropdown is set to default without clicking the dropdown. How can I overcome this issue?
CodePudding user response:
You can read the data directly
sendToBe({employee_id: this.createEmpForm.get('employee_id').value})
or send the whole form (or parts)
const formValue = this.form.value
sendToBe(formValue)
or start with current value
this.createEmpForm.get('employee_id').valueChanges.pipe(
startWith(this.createEmpForm.get('employee_id').value)
).subscribe(value => {
...
});