1 | initial version |
To set an initial value for number controls in Angular 14 typed forms, you can do the following steps:
import { FormGroup, FormControl } from '@angular/forms';
@Component({
selector: 'app-my-form',
templateUrl: './my-form.component.html',
styleUrls: ['./my-form.component.css']
})
export class MyFormComponent implements OnInit {
myForm: FormGroup;
ngOnInit() {
this.myForm = new FormGroup({
myNumberControl: new FormControl()
});
}
}
ngOnInit() {
this.myForm = new FormGroup({
myNumberControl: new FormControl(42)
});
}
Note: Replace 42
with the desired initial value for your number control.
<form [formGroup]="myForm">
<input type="number" formControlName="myNumberControl">
</form>
That's it! The number control will now have an initial value of 42, and you can change it using the input element.