Simple Login with Angular

Tue, Nov 12, 2019

Read in 2 minutes

Introduction :

In this blog, you will learn how to create a simple login using angular with bootstrap design.

loginangular-2-1024x504

Explanation :

For creating a login, I use the angular forms module. For this example, I use template-driven forms. We will talk about the reactive form later. To use the form module, you have to include a form module in the app.module.ts file.

App.module.ts : login1.png

Create a login component by ng g c login.

This will create login.component.ts, login.component.ts and login.component.css.

First, we will see login.component.html. login2.png

               <span class="errormsg"
              *ngIf="!username.valid && username.touched">
                  username required</span>

login.component.ts login3

Here, this.loginFrom gives the below output.

NgForm printed in the console login4

NgForm all properties login5

ng-form values uname and password login6

In login() function, we will check if the username and password are equal to test and if it is “test ” we will set a variable as success. This is just for this example. In real-time you will make a call to API and do this check.

Based on the “loginResult” variable we are displaying success message or error message.

login.component.css login7