Ask Your Question
3

What is the process to set an initial value for number controls in Angular 14 typed forms?

asked 2021-10-20 11:00:00 +0000

woof gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-02-25 18:00:00 +0000

pufferfish gravatar image

To set an initial value for number controls in Angular 14 typed forms, you can do the following steps:

  1. In your component file, initialize the form group and controls:
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()
    });
  }

}
  1. Set the initial value for the number control in the form group:
ngOnInit() {
  this.myForm = new FormGroup({
    myNumberControl: new FormControl(42)
  });
}

Note: Replace 42 with the desired initial value for your number control.

  1. Bind the form group to your template and the number control to an input element:
<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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-10-20 11:00:00 +0000

Seen: 11 times

Last updated: Feb 25 '22