Error while trying to fetch email and password from NgForm in angular

Viewed 61

The below TypeScript code in Angular 12 is giving me error when I run the server. Help

    const {email, password} from f.form.value;
    //TODO: do your checking here
    this.auth.signUp(email, password)
    .then((res) =>{
      this.router.navigateByUrl('/');
      this.toastr.success("Signup Success")
    })
    .catch((err) => {
      console.log(err.message);
      this.toastr.error("Signup failed")
    })
  }```
2 Answers

You can try this:

form : FormGroup = new FormGroup({
    email: new FormControl(),
    password: new FormControl()
})

In your HTML, you can bind it to the input like this:

<input matInput formControlName="email">
<input matInput formControlName="password">

Then access it using:

this.form.get('email').value
this.form.get('password').value
    const { email, password } = f.form.value;
    //TODO: do your checking here
    this.auth.signUp(email,password)
    .then((res) =>{
      this.router.navigateByUrl('/');
      this.toastr.success("Signup Success")
    })
    .catch((err) => {
      console.log(err.message);
      this.toastr.error("Signup failed")
    })
  }
Related