Angular comes with mainly two different ways of building forms in our applications. They are :
1. Template driven approach which allows to build forms with less cpde
2. Reactive approach using low level APIs which makes our forms testable without DOM. We can also use FormBuilder which is a high level API and is accessible to us in angular.
If you want to use the template driven approach. You need to first import the FormsModule in your app.module.ts file. Add the following statement in the file:
import { FormsModule } from '@angular/forms';
and in your @NgModule directive, add the FormsModule;
@NgModule({
imports:[
//other imports
FormsModule
],
})
Then in your login template, you need to make changes and the code should look like the following:
<form (ngSubmit)="loginUser($event)">
<input type="text" placeholder="Enter Username" id="username">
<input type="password" placeholder="Enter Password" id="password">
<button type="submit">Submit</button>
<form>
// It is good to be semantic and use buttons instead of input type = "submit"
and In login.component.ts remove the event.preventDefault(). and also, the syntax for console.log is with small 'c'. So, the code would look like this:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
constructor() { }
ngOnInit() {
}
loginUser(event) {
console.log(event);
}
}
//You do not need to add event.preventDefault() since you won't be redirected to any new URL.
Now you can see in console, that your event is being logged.
If you want to try the reactive approach (which is also called model driven). You will need to import the ReactiveFormsModule. So, add the following snippet of code to the app.mopdule.ts
import { ReactiveFormsModule } from '@angular/forms'
@NgModule({
imports:[
//other imports
ReactiveFormsModule
],
})
Remember, Reactive approach means you will need to model the form beforehand and implement in your template.
So, in your login.component.ts. you will need to import FormControl to set the initial value for the form. Your login.component.ts file would look like this:
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormControl } from '@angular/forms';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
loginForm = new FormGroup({
username: new FormControl(''),
password: new FormControl('')
});
constructor() { }
ngOnInit() {
}
loginUser() {
console.log(this.loginForm.value);
}
}
Now, Make changes to your login.component.html file to be the following:
<form [formGroup]="loginForm" (ngSubmit)="loginUser()">
<input type="text" placeholder="Enter Username" formControlName="username">
<input type="password" placeholder="Enter Password" formControlName="password">
<button type="submit">Submit</button>
</form>
Now you can see your console to see the values being stored.