I am struggling to create a dynamic form where the dropdown selection (control2) changes based on what is entered in a text box (control1). My goal is that when a user inputs a value into control1, I want control2 to display a specific string corresponding to that value, along with the option 'Date'. The dropdown should only have two values: 'Selected' and 'Date'. If the user enters an incorrect value in control1, the dropdown should display 'Selected' and reset the value of control2. However, I seem to be facing difficulties in displaying both 'Date' and 'Selected' options in the dropdown while updating control2 accordingly. Can someone please help me with this?
Form setup:
form: FormGroup = this.fb.group({
control1: (null),
control2: {value: null, disabled: true}
})
Function to check valid text entry:
validControl(): boolean {
if(this.control1.value !== null && this.control1.value !== ''
&& this.control1.valid) {
return true;
}
else {
return false;
}
Take a look at this stackblitz demo for a visual representation:
In summary, here is what I aim to achieve:
- User enters a valid value in the text box (control1)
- The dropdown displays 'Date' and binds 'EDTEV' to control2
- If the user deletes the text in control1, the dropdown shows 'Selected' and resets the value in control2