This tutorial will show how to build a beautiful login and registration UI with Angular Material. We’ll look at the various Material design components and how they can be used to bring about professional-looking applications. The complete code for this tutorial can be found in our GitHub repo.
Getting Started
We will start by creating an Angular application. Hopefully, you already have Node.JS and Angular CLI installed in your working environment.
Use the following command to create a new Angular app.
# create a new Angular project under the name responsive-angular-material ng new registration-login-angular-material
This command creates all the files needed to bootstrap an Angular Application. The next step will be to add Angular Material in your application.
cd responsive-angular-material/ #install angular material ng add @angular/material
Creating Components using Angular CLI
Components in Angular are the building blocks of the Application. Although you can manually create components, Angular CLI makes it very easy to automatically generate components that contain all the necessary starter code. To generate a component in Angular CLI, you simply run the following command:
# MyComponent is the name of your component ng g component MyComponent
Our app will have the following components:
- the
Registration
component - the
Login
Component
Let’s generate those components now.
#registration component ng g component RegistrationComponent #login component ng g component LoginComponent
Adding a Theme
The beauty of Angular Material is that it comes with pre-built themes, so you can easily bootstrap the look and feel of your application by simply plugging in some simple snippets of code to your app. To add a theme, simply import it to style.css.
/*style.css*/ @import "~@angular/material/prebuilt-themes/indigo-pink.css";
Building the UI
We want our UI to feature a navigation bar with links for registration and log in. So, we’ll create a navigation bar with two buttons that link to the app components.
To create a toolbar and buttons, you use the
and the
components respectively.
Go ahead and add the HTML code for the UI in src/app/app.component.html. Notice we have also added links to the routes.
HOME Register Login
Next, we will add some styling to our UI. Open app.component.css and add the following CSS styles.
.spacer { flex: 1 1 auto; }
Importing Angular Material Components.
You also need to import the modules from @angular/material
. You can choose to import them from the main module or create a separate module that will contain all the Material design modules. Since we will be working with many of these modules when creating the rest of the UI, we will create a separate module for all the components. Go ahead and create a new file src/app/material.module.ts.
Add the modules for the
, and the
components in the file as shown below.
import { NgModule } from '@angular/core'; import {MatButtonModule,MatToolbarModule} from '@angular/material'; @NgModule({ imports: [MatButtonModule,MatToolbarModule], exports: [MatButtonModule,MatToolbarModule], }) export class MyMaterialModule { }
Then, from other components in our code, we only need to include the module we just created—app/app.module.ts—as shown below.
// app/app.module.ts import { MyMaterialModule } from './material.module'; @NgModule({ imports: [ BrowserModule, BrowserAnimationsModule, MyMaterialModule, ], })
Issue ng serve
command to test your application and you should see a nice looking navigation area with Register and Login links.
Enable Routing
Routing enables users to navigate between different pages. To enable routing in our application, we will first define the routing configuration. To do that add the following code to app.module.ts.
import { RouterModule, Routes } from '@angular/router'; imports: [ BrowserModule, BrowserAnimationsModule, MyMaterialModule, RouterModule.forRoot([ { path: '', redirectTo: '/', pathMatch: 'full' }, { path: 'register', component: RegistrationComponentComponent }, { path: 'login', component: LoginComponentComponent }, ]), ],
The next step will be to add a
element which enables the Angular Router to know where to place the routed components. The app.component.html file should now look like this:
HOME Register Login
We’ve placed the router outlet so the components will render below the navigation bar.
Registration UI
In this section, we will mainly make use of the layout and form control components. This UI will showcase these Material Design components:
- card component (
)—a content container for text, photos, and actions in the context of a single subject - label component (
)—used to specify a label for the form field - input component (
)—specifies an input field
- form field component (
)—wraps several Angular components to make a form field - checkbox component (
)—a native checkbox with enhanced Material Design styling - date picker component (
)—an enhanced date picker
But first, let’s import them in src/app/material.ts.
import { NgModule } from '@angular/core'; import {MatNativeDateModule,MatDatepickerModule,MatIconModule,MatButtonModule,MatCheckboxModule, MatToolbarModule, MatCardModule,MatFormFieldModule,MatInputModule,MatRadioModule,MatListModule,} from '@angular/material'; import {MatDatepickerModule} from '@angular/material/datepicker'; import { FormsModule, ReactiveFormsModule } from '@angular/forms'; @NgModule({ imports: [MatNativeDateModule,MatDatepickerModule,MatIconModule,MatButtonModule,MatCheckboxModule, MatToolbarModule,FormsModule, MatCardModule,MatFormFieldModule,MatInputModule,MatListModule,MatRadioModule,], exports: [MatNativeDateModule,FormsModule, MatDatepickerModule,MatIconModule,MatButtonModule,MatCheckboxModule, MatToolbarModule, MatCardModule,MatFormFieldModule,MatInputModule,MatListModule,MatRadioModule,], }) export class MyMaterialModule { }
We will start with the
which will contain all the content in the registration UI. Open registration-component.component.html and add the following code.
Registration
Next, we will add an HTML form inside the content section that will contain all our form fields.
Registration
Next, we will add form fields for the first name, last name, address, email, and password. We will be using
to create labels and to create the input fields.
Next, we will add a checkbox for gender and a date picker for date of birth. We will use
and
which have enhanced Material Design styling and animations.
Male Female Other Date of Birth
The last bit will be a button after the for submitting the user information.
Form Styling
Lets put some styling on our forms to make more presentable. Open create-account.component.css and add the following CSS styles.
.my-form{ min-width: 150px; max-width: 500px; width: 100%; } .full-width { width: 100%; }
The Registration UI will look like this:
Building the UI for the Login Component
The login UI will have the following components:
- card component (
)—a content container for text, photos, and actions in the context of a single subject - input component (
)—specifies an input field
Just like we did for the registration UI, we will have a Material card component to house the login form.
The HTML code for the Login page is shown which contains two inputs for email and password credentials.
LOGIN
The finished UI will look like this:
This tutorial has covered most of the Angular Material components needed to make a fully functioning UI. As you have seen, Angular Material is very easy to use since all the components have predefined styles. This means you can quickly build beautiful UIs. Additionally, the Angular Material documentation provides a lot of examples and is easy to follow.
Powered by WPeMatico