I am facing an issue where I have an input field and a button. The button should be enabled when the field is not empty. However, even though I am already passing an input value, the button is not being enabled as expected. It only gets enabled when I actually type something in the input field. Does anyone have any advice on how to resolve this? Below is the code snippet:
export class SignupFormComponent implements OnInit {
userForm: FormGroup;
submitted: boolean;
hello = "hello world";
ngOnInit() {
this.userForm = new FormGroup({
firstName: new FormControl('',[<any>Validators.required, <any>Validators.minLength(5)])
});
}
onSubmit({ value, valid }: { value: userForm, valid: boolean }) {
this.submitted = true;
console.log(value,valid);
}
}
Here is my HTML code:
<form [formGroup]="userForm" (ngSubmit)="onSubmit(userForm)" validate>
<div class="form-group">
<label>First Name</label>
<input type="text" class="form-control" formControlName="firstName" [value]="hello">
</div>
<button type="submit" class="btn btn-primary" [disabled]="userForm.invalid">Submit </button>
</form>