-
Notifications
You must be signed in to change notification settings - Fork 27k
Description
Describe the problem that you experienced
The sample code for the FormBuilder in the reactive forms docs code provides the FormBuilder via constructor injection.
The form itself however is defined as a member, which references the provided FormBuilder when building the form.
TypeScript fails to compile as the formBuilder is used before initialization.
@Component({/*...*/})
export class AppComponent {
form = this.formBuilder.group({
firstname: ['']
});
constructor(private formBuilder: FormBuilder) {
}
// ...
}TS2729: Property 'formBuilder' is used before its initialization.
Enter the URL of the topic with the problem
https://angular.dev/guide/forms/reactive-forms#import-the-formbuilder-class
Describe what you were looking for in the documentation
No response
Describe the actions that led you to experience the problem
No response
Describe what you want to experience that would fix the problem
One way to fix this would be providing the example using the inject function.
@Component({/*...*/})
export class AppComponent {
formBuilder = inject(FormBuilder);
form = this.formBuilder.group({
firstname: ['']
})
}Another would be to set up the form in the constructor instead of the member.
@Component({/*...*/})
export class AppComponent {
form;
constructor(private formBuilder: FormBuilder) {
form = this.formBuilder.group({
firstname: ['']
});
}
}Add a screenshot if that helps illustrate the problem
No response
If this problem caused an exception or error, please paste it here
TS2729: Property 'formBuilder' is used before its initialization.
If the problem is browser-specific, please specify the device, OS, browser, and version
No response
Provide any additional information here in as much as detail as you can
The project is a fresh project created with the latest angular cli.