2

I have a sample project where I have used angular forms. I have created form group object and added form controls and all. But unable to see password field which is in the template and form is not working. Here is the code.

Stackbliz Link

app.component.ts

import { FormControl, FormGroup, Validators } from '@angular/forms';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  public form: FormGroup = new FormGroup({
    email: new FormControl('', [Validators.required]),
    password: new FormControl('', [Validators.required])
  });

  login() {
    if (this.form.valid) {
      console.log(this.form.value);
    }
  }
}

app.component.html

<h3>Login Form</h3>
<form (ngSubmit)="login()">
  <label>Email: </label>
  <input formControlName="email" type="text" />
  <br><br>
  <label>Password: </label>
  <input  formControlName="password" type="password" />
  <br><br>
  <button type="submit">Login</button>
</form>
1
  • 1
    Hi I think you missed to add formGroup attribute to the form tag and bind the formGroup object. Commented Jun 10, 2021 at 4:06

2 Answers 2

2

Add formGroup attribute in your form.

<form [formGroup]="form" (ngSubmit)="login()">...
Sign up to request clarification or add additional context in comments.

Comments

1

There are two things you have missed in the code one is to add formGroup as said by ammad and also you have to add Reactive forms module in app.module.ts file.

Stackbliz Link

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.