I am trying to set the input type number's default (initial) value as 1 but is having a difficult time figuring out how to do it. I know that I can set the value with value="1" if there is no formControlName. I tried inputNum: new FormControl(1, [Validators.required, Validators.pattern('^[1-9]\\d*$')])
and
inputNum: new FormControl([1, Validators.required, Validators.pattern('^[1-9]\\d*$')]) without success.
The input should have the initial value of 1 but I should be able to set it as 1 to whatever positive whole number.
<input formControlName="inputNum" class="form-control" [ngClass]="{'is-invalid':inputNumForm.touched && inputNumForm.errors}" type="number">
<div class="invalid-feedback" *ngIf="inputNumForm.touched && inputNumForm.errors">
<label *ngIf="inputNumForm.errors.required">Numbers Only</label>
</div>
public form = new FormGroup({
inputNum: new FormControl('', [Validators.required, Validators.pattern('^[1-9]\\d*$')]),
});
get inputNumForm(): FormControl {
return this.form.controls['inputNum'] as FormControl;
}
this.form.setValue(YOUR_VALUE)or you can do it directly on the element as there is avalueproperty, check: angular.io/api/forms/AbstractControl#setValue