Complete the code to import the ReactiveFormsModule in the Angular module.
import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { [1] } from '@angular/forms'; @NgModule({ declarations: [], imports: [BrowserModule, [1]], bootstrap: [] }) export class AppModule {}
You need to import ReactiveFormsModule from @angular/forms to use reactive forms in Angular.
Complete the code to create a FormGroup with a 'name' control using FormBuilder.
import { Component } from '@angular/core'; import { FormBuilder, FormGroup } from '@angular/forms'; @Component({ selector: 'app-profile', template: `<form [formGroup]="profileForm"></form>` }) export class ProfileComponent { profileForm: FormGroup; constructor(private fb: FormBuilder) { this.profileForm = this.fb.group({ name: [1] }); } }
The FormBuilder.group method expects each control to be an array with the initial value and optionally validators. Here, [''] initializes the 'name' control with an empty string.
Fix the error in the template binding to connect the form group correctly.
<form [[1]]="profileForm"> <input formControlName="name"> </form>
The formGroup directive binds the form element to the FormGroup instance in the component.
Fill both blanks to import and inject FormBuilder in the component.
import { Component } from '@angular/core'; import { [1] } from '@angular/forms'; @Component({ selector: 'app-login', template: `<form [formGroup]="loginForm"></form>` }) export class LoginComponent { loginForm; constructor(private [2]: FormBuilder) { this.loginForm = this.[2].group({}); } }
You import FormBuilder and inject it with a variable name, here fb, to create form groups easily.
Fill all three blanks to create a FormGroup with 'email' and 'password' controls initialized as empty strings.
this.loginForm = this.fb.group({
[1]: [2],
[3]: ['']
});Each control is a key with an array containing the initial value. Here, 'email' and 'password' are initialized with empty strings.