Documente Academic
Documente Profesional
Documente Cultură
Techiediaries
Ad
In this tutorial, we'll learn how to use Angular forms in Ionic 4 by building a simple login and registration
example with theming.
In the previous tutorial, we've created a JWT authentication server with Node and Express.js and
implemented an authentication service with Angular services and HttpClient in our Ionic 4
application.
Check out the previous tutorial: Ionic 4 JWT Authentication Tutorial: Using Angular
HttpClient with Node & Express.js Server
In this part, we'll create the actual UI with built-in Ionic 4 components and Angular forms.
https://www.techiediaries.com/ionic-ui-forms-theming/ 1/15
21/4/2019 Ionic 4 Forms Tutorial: Login & Register UI Example with Theming | Techiediaries
We'll be using Ionic 4 components such as the Ionic Grid, Input fields and Buttons. x
We'll also see how to use some CSS variables like --background and --color for custom theming
the UI components and the color property of Ionic components to assign different types of standard Ionic
colors such as the primary and secondary colors to the components.
Install weForms- the best HTML contact form builder plugin for
your website. wedevs.com
We'll see how to use Angular routing to navigate between different pages of our Ionic 4 application.
We've created an Ionic 4 project, created a module that encapsulates authentication and created a service
that implements the register() , login() , logout() and isLoggedIn() methods. Let's now
create the UI pages and components.
src/app/auth/register/register.module.ts ,
src/app/auth/register/register.page.scss ,
src/app/auth/register/register.page.html ,
src/app/auth/register/register.page.spec.ts ,
src/app/auth/register/register.page.ts
https://www.techiediaries.com/ionic-ui-forms-theming/ 2/15
21/4/2019 Ionic 4 Forms Tutorial: Login & Register UI Example with Theming | Techiediaries
It will also update the src/app/app-routing.module.ts file to enable routing to this page by adding x
the following route:
Open the src/app/auth/register/register.page.ts file and import then inject AuthService and
Router :
ngOnInit() {
}
Next, add the register() method that will be called for registering users:
register(form) {
this.authService.register(form.value).subscribe((res) => {
this.router.navigateByUrl('home');
});
}
This method simply calls the register() method of AuthService , subscribe to the returned
Observable and navigate to the home page when registration is done.
We use the navigateByUrl() method of the Angular Router to navigate to a page by its URL.
https://www.techiediaries.com/ionic-ui-forms-theming/ 3/15
21/4/2019 Ionic 4 Forms Tutorial: Login & Register UI Example with Theming | Techiediaries
The register() method takes an Angular form object. The value variable contains a JS object that x
corresponds to the fields of the form and their values.
Next open the src/auth/register/register.page.html file and add a form inside <ion-
content> :
<ion-content color="primary">
<form #form="ngForm" (ngSubmit)="register(form)">
<ion-grid>
<ion-row color="primary" justify-content-center>
<ion-col align-self-center size-md="6" size-lg="5" size-xs="12">
<div text-center>
<h3>Create your account!</h3>
</div>
<div padding>
<ion-item>
x
<ion-input name="name" type="text" placeholder="Name" ngModel require
</ion-item>
<ion-item>
<ion-input name="email" type="email" placeholder="your@email.com" ngMo
</ion-item>
<ion-item>
<ion-input name="password" type="password" placeholder="Password" ngMo
</ion-item>
<ion-item>
<ion-input name="confirm" type="password" placeholder="Password again"
</ion-item>
</div>
<div padding>
<ion-button size="large" type="submit" [disabled]="form.invalid" expand
</div>
</ion-col>
</ion-row>
</ion-grid>
</form>
</ion-content>
Next, let's add some styling. Open the src/auth/register/register.page.scss file and add:
https://www.techiediaries.com/ionic-ui-forms-theming/ 4/15
21/4/2019 Ionic 4 Forms Tutorial: Login & Register UI Example with Theming | Techiediaries
x
ion-item{
--background: #3880ff;
--color: #fff;
}
ion-button{
--background: #062f77;
}
We use the --background and --color variables to change the colors of <ion-item> and <ion-
button> components.
https://www.techiediaries.com/ionic-ui-forms-theming/ 5/15
21/4/2019 Ionic 4 Forms Tutorial: Login & Register UI Example with Theming | Techiediaries
src/app/auth/login/login.module.ts ,
src/app/auth/login/login.page.scss ,
src/app/auth/login/login.page.html ,
src/app/auth/login/login.page.spec.ts ,
src/app/auth/login/login.page.ts
Now, open the src/app/auth/login/login.page.ts file, import and inject both AuthService and
Router :
https://www.techiediaries.com/ionic-ui-forms-theming/ 6/15
21/4/2019 Ionic 4 Forms Tutorial: Login & Register UI Example with Theming | Techiediaries
x
import { Component, OnInit } from '@angular/core';
import { Router } from "@angular/router";
import { AuthService } from '../auth.service';
@Component({
selector: 'app-login',
templateUrl: './login.page.html',
styleUrls: ['./login.page.scss'],
})
export class LoginPage implements OnInit {
ngOnInit() {
}
x
}
login(form){
this.authService.login(form.value).subscribe((res)=>{
this.router.navigateByUrl('home');
});
}
The login() method simply calls the login() method of AuthService and subscribe to the
returned Observable then navigate to the home page when login is done.
Let's now create the login UI. Open the src/app/auth/login/login.page.html file and add the
following code:
https://www.techiediaries.com/ionic-ui-forms-theming/ 7/15
21/4/2019 Ionic 4 Forms Tutorial: Login & Register UI Example with Theming | Techiediaries
ion-item{
--background: #3880ff;
--color: #fff;
}
ion-button{
--background: #062f77;
}
Next, let's add a link to the register page to allow users to register if they don't already have an account.
Inside the <ion-grid> component add:
<ion-row>
<div text-center>
If you don't have an account, please <a routerLink='/register'>
register</a> first!
</div>
</ion-row>
We use the routerLink directive of Angular Router to create a link to the register page.
https://www.techiediaries.com/ionic-ui-forms-theming/ 8/15
21/4/2019 Ionic 4 Forms Tutorial: Login & Register UI Example with Theming | Techiediaries
x
a{
color: #fff;
}
https://www.techiediaries.com/ionic-ui-forms-theming/ 9/15
21/4/2019 Ionic 4 Forms Tutorial: Login & Register UI Example with Theming | Techiediaries
Conclusion
That's it we have created our login and registration system using Ionic 4 and Angular 7 in the front-end
x
and Node, Express.js in the backend.
22 Jan 2019
ionic
« Ionic 4 JWT Authentication Tutorial: Using Angular HttpClient with Node & Express.js
Server
Angular 7|6 Tutorial: Building and Submitting a Form (ngModel | ngForm | ngSubmit) to a
Node and Express.js Authentication Server »
https://www.techiediaries.com/ionic-ui-forms-theming/ 10/15
21/4/2019 Ionic 4 Forms Tutorial: Login & Register UI Example with Theming | Techiediaries
Author
Team
Techiediaries is a website
dedicated to bring you
tutorials for the latest web
technologies
report this ad
RELATED TUTORIALS
How to start a website from A to Z: A 5-step guide [Sponsored]
Ionic 4 React Tutorial: Build a Mobile App with Ionic 4, Axios and React
Build a CRM App with Ionic 4/Angular and TypeORM: Custom Webpack Configuration
[Part 1]
Ionic 4 JWT Authentication Tutorial: Using Angular HttpClient with Node & Express.js
Server
https://www.techiediaries.com/ionic-ui-forms-theming/ 12/15
21/4/2019 Ionic 4 Forms Tutorial: Login & Register UI Example with Theming | Techiediaries
SUBSCRIBE
x
AD
https://www.techiediaries.com/ionic-ui-forms-theming/ 13/15
21/4/2019 Ionic 4 Forms Tutorial: Login & Register UI Example with Theming | Techiediaries
ALSO ON TECHIEDIARIES
Ionic 4 Tutorial: Your First Ionic/Angular v4 Nest.js Tutorial: JWT Authentication with
Application Passport.js
4 comments • 4 months ago 1 comment • 2 months ago
Jonathan Tolbert — You right.. I just felt part I Rodrigo Correa — how can I patch the
Avatarwas so good, I'm eager to continue.. Avatarpassword?
✉ Subscribe d Add Disqus to your siteAdd DisqusAdd 🔒 Disqus' Privacy PolicyPrivacy PolicyPrivacy
Custom Search
https://www.techiediaries.com/ionic-ui-forms-theming/ 15/15