Getting Started | Building an Application with Ionic
In this part 2 of enterprise application we will buid an application with Ionic framewotk
The objective of this tutorial is how to know Design Patterns work. In this tutorial we will create an application enterprise to calculate the language and the level for the applications how many language used in the app like Java, Php, Javascript etc... Criticality like Low, Medium and High.
Ionic apps are created and developed primarily through the Ionic command line utility (the “CLI”), and use Cordova to build/deploy as a native app. This means we need to install a few utilities to get developing.
Installing Ionic
Ionic apps are created and developed primarily through the Ionic command-line utility. The Ionic CLI is the preferred method of installation, as it offers a wide range of dev tools and help options along the way. It is also the main tool through which to run the app and connect it to other services, such as Ionic Appflow.
Before proceeding, make sure your computer has Node.js installed.
Install the Ionic CLI with npm:
$ npm install -g @ionic/cli
Starting an App
Starting a new Ionic app is incredibly simple. From the command line, run the ionic start command and the CLI will handle the rest.
Here, myApp is the name of the project, blank is the starter template, and the project type is angular.
$ ionic start enterprise tabs
Project structure
Let's go through the files and folders of the app.
/ src
Inside of the /src directory we find our raw, uncompiled code. This is where most of the work for your Ionic app will take place.
/ app
The App folder is the largest folder because it contains all the code of our ionic app. It has all the components, modules, pages, services and styles you will use to build your app.
This is the core of the project. Let’s have a look at the structure of this folder so you get an idea where to find things and where to add your own modules to adapt this project to your particular needs.
Modals
Create a folder modal in side it add a class Model.ts
src/app/modal/Modal.ts
export interface User {
id: number;
name: string;
}
export interface Application {
id: number;
name: string;
description: string;
criticality: Criticality;
language: Language;
}
export interface Enterprise {
id: number;
name: string;
description: string;
applications: Application[];
}
export enum Criticality {
LOW,
MEDIUM,
HIGH,
}
export enum Language {
JAVA,
JAVASCRIPT,
PHP,
}
Angular Services
Angular services are singleton objects that get instantiated only once during the lifetime of an application. They contain methods that maintain data throughout the life of an application, i.e. data does not get refreshed and is available all the time. The main objective of a service is to organize and share business logic, models, or data and functions with different components of an Angular application.
An example of when to use services would be to transfer data from one controller to another custom service.
Why Should We Use Services in Angular? The separation of concerns is the main reason why Angular services came into existence. An Angular service is a stateless object and provides some very useful functions. These functions can be invoked from any component of Angular, like Controllers, Directives, etc. This helps in dividing the web application into small, different logical units which can be reused.
Create these service in the service package
-> ApplicationService-> EnterpriseService
-> UserService
src/app/service/application-service.ts
import { HttpClient } from '@angular/common/http';
import { Injectable} from '@angular/core';
import { Observable} from 'rxjs';
import { Application} from '../modal/Modal';
@Injectable({
providedIn: 'root',
})
export class ApplicationService {
constructor(private http: HttpClient) { }
createApplication(application: Application, id: number): Observable<any> {
return this.http.post<any>(
`http://localhost:8080/api/createApplication/${id}`,
application
);
}
createApplicationForEnterprise(idApplication: number, id: number): Observable<any> {
return this.http.post<any>(
`http://localhost:8080/api/createApplicationForEnterprise/${idApplication}/${id}`,
null
);
}
deleteApplication(id: number): Observable<any> {
return this.http.delete<any>(
`http://localhost:8080/api/deleteApplication/${id}`
);
}
findApplication(id: number): Observable<any> {
return this.http.get<any>(
`http://localhost:8080/api/findApplication/${id}`
);
}
findApplications(): Observable<any[]> {
return this.http.get<any[]>(
`http://localhost:8080/api/findApplications`);
}
findApplicationsForEnterprise(id: number): Observable<any[]> {
return this.http.get<any[]>(
`http://localhost:8080/api/findApplicationsForEnterprise/${id}`);
}
}
src/app/service/enterprise-service.ts
import { HttpClient } from '@angular/common/http';
import { Injectable} from '@angular/core';
import { Observable} from 'rxjs';
import { Enterprise} from '../modal/Modal';
@Injectable({
providedIn: 'root',
})
export class EnterpriseService {
constructor(private http: HttpClient) { }
createEnterprise(enterprise: Enterprise): Observable<any> {
return this.http.post<any>(
`http://localhost:8080/api/createEnterprise`,
enterprise
);
}
deleteEnterprise(id: number): Observable<any> {
return this.http.delete<any>(
`http://localhost:8080/api/deleteEnterprise/${id}`
);
}
findEnterprise(id: number): Observable<any> {
return this.http.get<any>(
`http://localhost:8080/api/findEnterprise/${id}`
);
}
findEnterprises(): Observable<any[]> {
return this.http.get<any[]>(
`http://localhost:8080/api/findEnterprises`);
}
findApplicationsForEnterprise(id: number): Observable<any[]> {
return this.http.get<any[]>(
`http://localhost:8080/api/findApplicationsForEnterprise/${id}`);
}
calculateJava(id: number): Observable<any> {
return this.http.get<any>(
`http://localhost:8080/api/calculateJava/${id}`);
}
calculateLow(id: number): Observable<any> {
return this.http.get<any>(
`http://localhost:8080/api/calculateLow/${id}`);
}
calculateMedium(id: number): Observable<any> {
return this.http.get<any>(
`http://localhost:8080/api/calculateMedium/${id}`);
}
calculatePhp(id: number): Observable<any> {
return this.http.get<any>(
`http://localhost:8080/api/calculatePhp/${id}`);
}
calculateHigh(id: number): Observable<any> {
return this.http.get<any>(
`http://localhost:8080/api/calculateHigh/${id}`);
}
calculateJavascript(id: number): Observable<any> {
return this.http.get<any>(
`http://localhost:8080/api/calculateJavascript/${id}`);
}
}
src/app/service/user-service.ts
import { HttpClient } from '@angular/common/http';
import { Injectable} from '@angular/core';
import { Observable} from 'rxjs';
import { User} from '../modal/Modal';
@Injectable({
providedIn: 'root',
})
export class UserService {
constructor(private http: HttpClient) { }
createUser(user: User): Observable<any> {
return this.http.post<any>(
`http://localhost:8080/api/createUser`,
user
);
}
findUsers(): Observable<any[]> {
return this.http.get<any[]>(
`http://localhost:8080/api/findUsers`);
}
}
Create pages
Now we'll continue with the project by adding pages which are the basic buildings of an Ionic app. So let's get started. You can create pages either manually or generating them using the Ionic CLI v5 which is the recommended method. In this guide we'll look first at how to create a page generate it with the Ionic CLI, then how to add it to the project.
We will use these pages
- add-user.page.ts- add-user.page.html
src/app/add-user.page.ts
import { Component, OnInit } from '@angular/core';
import { User } from '../model/Model';
import { UserService } from '../service/user.service';
@Component({
selector: 'app-add-user',
templateUrl: './add-user.page.html',
styleUrls: ['./add-user.page.scss']
})
export class AddUserPage implements OnInit {
users: User[];
user: User = {} as User;
progressBar = false;
constructor(private userService: UserService) { }
ngOnInit() {
this.userService.findUsers().subscribe(users => {
this.users = users;
});
}
addUser() {
this.showProgressBar = true;
this.userService.createUser(this.user).subscribe(user => {
this.user = user;
window.location.reload();
})
}
}
src/app/add-user.page.html
<ion-header [translucent]="true">
<ion-toolbar color="dark">
<ion-title>Users</ion-title>
</ion-toolbar>
</ion-header>
<ion-content [fullscreen]="true">
<ion-card>
<ion-spinner name="bubbles" color="primary" class="center" *ngIf="progressBar"></ion-spinner>
<ion-card-content>
<form name="user" (ngSubmit)="addUser()" #formRegister="ngForm" novalidate>
<ion-item>
<ion-label position="floating">Name</ion-label>
<ion-input type="text" placeholder="Enter name" name="name" [(ngModel)]="user.name" #name="ngModel" required></ion-input>
</ion-item><br>
<ion-button type="submit" color="success" fill="solid" size="large" expand="block" [disabled]="name.invalid">Save</ion-button>
</form>
</ion-card-content>
</ion-card>
<ion-card>
<ion-card-header>
<ion-card-subtitle>Find all users</ion-card-subtitle>
</ion-card-header>
<ion-card-content>
<ion-list *ngFor="let u of users">
<ion-item lines="full">
<ion-label>
{{u.name}}
</ion-label>
<ion-icon color="success" slot="end" name="create-outline" style="cursor: pointer;" [routerLink]="['/add-application/', u.id]"></ion-icon>
</ion-item>
</ion-list>
</ion-card-content>
</ion-card>
</ion-content>
We will use these pages
- add-application.page.ts- add-application.page.html
src/app/add-application.page.ts
import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { Application} from '../model/Model';
import { ApplicationService} from '../service/application.service';
@Component({
selector: 'app-add-application',
templateUrl: './add-application.page.html',
styleUrls: ['./add-application.page.scss']
})
export class AddApplicationPage implements OnInit {
progressBar = false;
application: Application = {} as Application;
criticality: any;
language: any;
id: number;
constructor(private applicationService: ApplicationService, private router: Router,
private route: ActivatedRoute) { }
ngOnInit() {
this.id = this.route.snapshot.params.id;
}
setCriticality(e) {
this.criticality = e.target.value;
}
setLanguage(e) {
this.language = e.target.value;
}
addApplication() {
this.showProgressBar = true;
this.application.criticality = this.criticality;
this.application.language = this.language;
this.applicationService.createApplication(this.application, this.id).subscribe(application => {
this.application = application;
window.location.reload();
})
}
cancel() {
this.router.navigateByUrl("/tabs/add-user")
}
}
src/app/add-application.page.html
<ion-header>
<ion-toolbar color="dark">
<ion-buttons slot="start">
<ion-button (click)="cancel()">
<ion-icon slot="icon-only" name="close-circle"></ion-icon>
</ion-button>
</ion-buttons>
<ion-title>Back</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
<ion-card>
<ion-spinner name="bubbles" color="primary" class="center" *ngIf="progressBar"></ion-spinner>
<ion-card-content>
<form name="application" (ngSubmit)="addApplication()" #formRegister="ngForm" novalidate>
<ion-item>
<ion-label position="floating">Name</ion-label>
<ion-input type="text" placeholder="Enter name" name="name" [(ngModel)]="application.name" #name="ngModel" required></ion-input>
</ion-item>
<ion-item>
<ion-label position="floating">Description</ion-label>
<ion-textarea type="text" placeholder="Enter description" name="description" [(ngModel)]="application.description"
#description="ngModel" required></ion-textarea>
</ion-item>
<ion-list>
<ion-radio-group value="criticality" (ionChange)="setCriticality($event)">
<ion-list-header>
<ion-label>Criticality</ion-label>
</ion-list-header>
<ion-item>
<ion-label>LOW</ion-label>
<ion-radio slot="start" value="LOW"></ion-radio>
</ion-item>
<ion-item>
<ion-label>MEDIUM</ion-label>
<ion-radio slot="start" value="MEDIUM"></ion-radio>
</ion-item>
<ion-item>
<ion-label>HIGH</ion-label>
<ion-radio slot="start" value="HIGH"></ion-radio>
</ion-item>
</ion-radio-group>
</ion-list>
<ion-list>
<ion-radio-group value="language" (ionChange)="setLanguage($event)">
<ion-list-header>
<ion-label>Language</ion-label>
</ion-list-header>
<ion-item>
<ion-label>JAVA</ion-label>
<ion-radio slot="start" value="JAVA"></ion-radio>
</ion-item>
<ion-item>
<ion-label>JAVASCRIPT</ion-label>
<ion-radio slot="start" value="JAVASCRIPT"></ion-radio>
</ion-item>
<ion-item>
<ion-label>PHP</ion-label>
<ion-radio slot="start" value="PHP"></ion-radio>
</ion-item>
</ion-radio-group>
</ion-list>
<br>
<ion-button type="submit" color="success" fill="solid" size="large" expand="block" [disabled]="name.invalid">Save</ion-button>
</form>
</ion-card-content>
</ion-card>
</ion-content>
We will use these pages
- add-enterprise.page.ts- add-enterprise.page.html
src/app/add-enterprise.ts
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { Enterprise } from '../model/Model';
import { EnterpriseService } from '../service/enterprise.service';
@Component({
selector: 'app-add-enterprise',
templateUrl: './add-enterprise.page.html',
styleUrls: ['./add-enterprise.page.scss']
})
export class AddEnterprisePage implements OnInit {
enterprise: Enterprise = {} as Enterprise;
progressBar = false;
constructor(private router: Router, private enterpriseService: EnterpriseService) { }
ngOnInit(): void { }
addEnterprise() {
this.showProgressBar = true;
this.enterpriseService.createEnterprise(this.enterprise).subscribe(enterprise => {
this.enterprise = enterprise;
window.location.reload();
})
}
cancel() {
this.router.navigateByUrl("/tabs/find-enterprises");
}
}
src/app/add-enterprise.html
<ion-header>
<ion-toolbar color="dark">
<ion-buttons slot="start">
<ion-button (click)="cancel()">
<ion-icon slot="icon-only" name="close-circle"></ion-icon>
</ion-button>
</ion-buttons>
<ion-title>Back</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
<ion-card>
<ion-spinner name="bubbles" color="primary" class="center" *ngIf="progressBar"></ion-spinner>
<ion-card-content>
<form name="application" (ngSubmit)="addEnterprise()" #formRegister="ngForm" novalidate>
<ion-item>
<ion-label position="floating">Name</ion-label>
<ion-input type="text" placeholder="Enter name" name="name" [(ngModel)]="enterprise.name" #name="ngModel" required></ion-input>
</ion-item>
<ion-item>
<ion-label position="floating">Description</ion-label>
<ion-textarea type="text" placeholder="Enter description" name="description" [(ngModel)]="enterprise.description"
#description="ngModel" required></ion-textarea>
</ion-item>
<br>
<ion-button type="submit" color="success" fill="solid" size="large" expand="block" [disabled]="name.invalid">Save</ion-button>
</form>
</ion-card-content>
</ion-card>
</ion-content>
We will use these pages
- find-enterprises.page.ts- find-enterprises.page.html
src/app/find-enterprises.ts
import { Component, OnInit } from '@angular/core';
import { Enterprise } from '../model/Model';
import { EnterpriseService } from '../service/enterprise.service';
@Component({
selector: 'app-find-enterprises',
templateUrl: './find-enterprises.page.html',
styleUrls: ['./find-enterprises.page.scss']
})
export class FindEnterprisesPage implements OnInit {
enterprises: Enterprise[];
constructor(private enterpriseService: EnterpriseService) { }
ngOnInit() {
this.enterpriseService.findEnterprises().subscribe(enterprises => {
this.enterprises = enterprises;
})
}
}
src/app/find-enterprises.html
<ion-header [translucent="true">
<ion-toolbar color="dark">
<ion-title>
Enterprises
</ion-title>
<ion-buttons slot="primary" [routerLink]="['/add-enterprise']">
<ion-button fill="solid" color="secondary">
Add enterprise
<ion-icon slot="end" name="create-outline"></ion-icon>
</ion-button>
</ion-buttons>
</ion-toolbar>
</ion-header>
<ion-content [fullscreen]="true">
<ion-card>
<ion-card-header>
<ion-card-title>Find all enterprises</ion-card-title>
</ion-card-header>
<ion-card-content>
<ion-list *ngFor="let e of enterprises">
<ion-item button [routerLink]="['/find-enterprise/', e.id]" color="secondary">
<ion-label>
{{e.name}}
</ion-label>
</ion-item>
</ion-list>
</ion-card-content>
</ion-card>
</ion-content>
We will use these pages
- find-enterprise.page.ts- find-enterprise.page.html
- find-enterprise.page.scss
src/app/find-enterprise.page.ts
import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { Enterprise, Application } from '../model/Model';
import { ApplicationService } from '../service/application.service';
import { EnterpriseService } from '../service/enterprise.service';
@Component({
selector: 'app-find-enterprise',
templateUrl: './find-enterprise.page.html',
styleUrls: ['./find-enterprise.page.scss']
})
export class FindEnterprisePage implements OnInit {
applications: Application[];
filterApps: Application[];
enterprise: Enterprise = {} as Enterprise;
id: number;
calculateJava: number;
calculateJavascript: number;
calculatePhp: number;
calculateLow: number;
calculateMedium: number;
calculateHigh: number;
constructor(private enterpriseService: EnterpriseService, private route: ActivatedRoute,
private applicationService: ApplicationService, private router: Router) { }
ngOnInit() {
this.id = this.route.snapshot.params.id;
this.enterpriseService.findEnterprise(this.id).subscribe(enterprise => {
this.enterprise = enterprise;
});
this.applicationService.findApplications().subscribe(applications => {
this.applications = applications;
this.applicationService.findApplicationsForEnterprise(this.id).subscribe(filterApps => {
this.filterApps = filterApps;
filterApps.forEach(app => {
this.applications = this.applications.filter(a => a.id !== app.id)
})
})
})
this.getCalculate();
}
setApplication(e) {
this.applicationService.createApplicationForEnterprise(e.target.value, this.id).subscribe(() => {
window.location.reload();
})
}
getCalculate() {
this.enterpriseService.calculateJava(this.id).subscribe(calculateJava => {
this.calculateJava = calculateJava;
});
this.enterpriseService.calculateJavascript(this.id).subscribe(calculateJavascript => {
this.calculateJavascript = calculateJavascript;
});
this.enterpriseService.calculatePhp(this.id).subscribe(calculatePhp => {
this.calculatePhp = calculatePhp;
});
this.enterpriseService.calculateLow(this.id).subscribe(calculateLow => {
this.calculateLow = calculateLow;
});
this.enterpriseService.calculateMedium(this.id).subscribe(calculateMedium => {
this.calculateMedium = calculateMedium;
})
this.enterpriseService.calculateHigh(this.id).subscribe(calculateHigh => {
this.calculateHigh = calculateHigh;
})
}
cancel() {
this.router.navigateByUrl("/tabs/find-enterprises");
}
deleteEnterprise() {
if(confirm('Are you sure'))
this.enterpriseService.deleteEnterprise(this.id).subscribe(() => {
window.location.replace('/tabs/find-enterprises')
})
}
}
src/app/find-enterprise.page.ts
<ion-header>
<ion-toolbar color="dark">
<ion-buttons slot="start">
<ion-button (click)="cancel()">
<ion-icon slot="icon-only" name="close-circle"></ion-icon>
</ion-button>
</ion-buttons>
<ion-title>Back</ion-title>
</ion-toolbar>
</ion-header>
<ion-content [fullscreen]="true">
<div class="stories">
<div class="story">
<div class="img-box">
<div class="content">{{calculateJava}}</div>
</div>
<p style="color: #86888f;">JAVA</p>
</div>
<div class="story">
<div class="img-box">
<div class="content">{{calculateJavascript}}</div>
</div>
<p style="color: #86888f;">JAVASCRIPT</p>
</div>
<div class="story">
<div class="img-box">
<div class="content">{{calculatePhp}}</div>
</div>
<p style="color: #86888f;">PHP</p>
</div>
<div class="story">
<div class="img-box">
<div class="content">{{calculateLow}}</div>
</div>
<p style="color: #86888f;">LOW</p>
</div>
<div class="story">
<div class="img-box">
<div class="content">{{calculateMedium}}</div>
</div>
<p style="color: #86888f;">MEDIUM</p>
</div>
<div class="story">
<div class="img-box">
<div class="content">{{calculateHigh}}</div>
</div>
<p style="color: #86888f;">HIGH</p>
</div>
</div>
<hr>
<ion-content>
<ion-card>
<ion-card-header>
<ion-card-subtitle>{{enterprise.name}}</ion-card-subtitle>
<ion-card-subtitle>Enterprise details</ion-card-subtitle>
<ion-card-subtitle>{{enterprise.description}}</ion-card-subtitle>
</ion-card-header>
<ion-card-content>
<ion-text color="danger">
<h1 style="margin-left: 15px;">Applications</h1>
</ion-text>
<ion-item>
<ion-label disabled="true">Add application</ion-label>
<ion-select (ionChange)="setApplication($event)">
<ion-select-option *ngFor="let app of applications" [value]="app.id">{{app.name}}</ion-select-option>
</ion-select>
</ion-item>
<ion-list *ngFor="let app of filterApps">
<ion-item color="dark">{{app.name}}</ion-item>
<ion-item *ngIf="app.criticality==['LOW']" color="success">{{app.criticality}}</ion-item>
<ion-item *ngIf="app.criticality==['MEDIUM']" color="secondary">{{app.criticality}}</ion-item>
<ion-item *ngIf="app.criticality==['HIGH']" color="danger">{{app.criticality}}</ion-item>
<ion-item color="medium">{{app.language}}</ion-item>
</ion-list>
</ion-card-content>
</ion-card><br>
<ion-button expand="full" fill="outline" color="danger" (click)="deleteEnterprise()">Delete this enterprise</ion-button>
</ion-content>
src/app/find-enterprise.page.scss
.text-stories {
display: flex;
justify-content: space-between;
font-size: smaller;
font-weight: 700;
color: #171717;
margin-left: 7px;
margin-right: 7px;
}
.stories {
display: flex;
overflow: scroll;
margin-left: 15px;
}
.stories .img-box {
margin-left: 7px;
margin-right: 12px;
background-image: url("https://drissas.com/wp-content/uploads/2019/11/instagram-bg-color.jpg");
background-size: contain;
border-radius: 50%;
}
.stories .content {
width: 50px;
max-width: none;
height: 50px;
max-height: none;
border-radius: 50%;
overflow: hidden;
border: 2px solid white;
margin-bottom: -3px;
text-align: center;
font-size: 22px;
color: aliceblue;
font-weight: bold;
margin-top: 2px;
padding-top: 8px;
}
.stories p {
font-size: 11px;
text-align: center;
margin: 4px 0px 0px 0px;
font-weight: 500;
color: #383a3e;
}
.add_story {
position: relative;
margin-left: 7px;
margin-right: 12px;
border-radius: 50%;
padding: 2px;
}
.add_story ion-icon {
position: absolute;
bottom: 1px;
right: 1px;
border-radius: 50%;
background-color: white;
}
hr {
height: 2px !important;
width: 100% !important;
background: #d5d5d5 !important;
}
We will use these pages
- find-applications.page.ts- find-applications.page.html
src/app/find-applications.page.ts
import { Component, OnInit } from '@angular/core';
import { Application } from '../model/Model';
import { ApplicationService } from '../service/application.service';
@Component({
selector: 'app-find-applications',
templateUrl: './find-applications.page.html',
styleUrls: ['./find-applications.page.scss']
})
export class FindApplicationsPage implements OnInit {
applications: Application[];
constructor(private applicationService: ApplicationService) { }
ngOnInit() {
this.applicationService.findApplications().subscribe(applications => {
this.applications = applications;
})
}
}
src/app/find-applications.page.html
<ion-header [translucent]="true">
<ion-toolbar color="dark">
<ion-title>Applications</ion-title>
</ion-toolbar>
</ion-header>
<ion-content [fullscreen]="true">
<ion-card>
<ion-card-header>
<ion-card-subtitle>Find all application</ion-card-subtitle>
</ion-card-header>
<ion-card-content>
<ion-list *ngFor="let app of applications">
<ion-item button [routerLink]="['/find-application/', app.id]" color="tertiary">
<ion-label>
{{app.name}}
</ion-label>
</ion-item>
</ion-list>
</ion-card-content>
</ion-card>
</ion-content>
We will use these pages
- find-application.page.ts- find-application.page.html
src/app/find-application.page.ts
import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { Application } from '../model/Model';
import { ApplicationService } from '../service/application.service';
@Component({
selector: 'app-find-application',
templateUrl: './find-application.page.html',
styleUrls: ['./find-application.page.scss']
})
export class FindApplicationPage implements OnInit {
application: Application = {} as Application;
id: number;
constructor(private router: Router, private route: ActivatedRoute,
private applicationService: ApplicationService) { }
ngOnInit() {
this.id = this.route.snapshot.params.id;
this.applicationService.findApplication(this.id).subscribe(application => {
this.application = application;
})
}
cancel() {
this.router.navigateByUrl("/tabs/find-applications")
}
deleteApplication() {
if(confirm('Are you sure')) {
this.applicationService.deleteApplication(this.id).subscribe(() => {
window.location.replace('/tabs/find-applications')
})
}
}
}
src/app/find-application.page.html
<ion-header>
<ion-toolbar color="dark">
<ion-buttons slot="start">
<ion-button (click)="cancel()">
<ion-icon slot="icon-only" name="close-circle"></ion-icon>
</ion-button>
</ion-buttons>
<ion-title>Back</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
<ion-card>
<ion-card-header>
<ion-card-title>{{application.name}}</ion-card-title>
<ion-card-subtitle>{{application.description}}</ion-card-subtitle>
</ion-card-header>
<ion-card-content>
<ion-list>
<ion-item *ngIf="application.criticality==['LOW']" color="success">{{application.criticality}}</ion-item>
<ion-item *ngIf="application.criticality==['MEDIUM']" color="secondary">{{application.criticality}}</ion-item>
<ion-item *ngIf="application.criticality==['HIGH']" color="danger">{{application.criticality}}</ion-item>
<ion-item color="medium">{{application.language}}</ion-item>
</ion-list>
</ion-card-content>
</ion-card>
<ion-button expand="full" fill="outline" color="danger" (click)="deleteApplication()">Delete this enterprise</ion-button>
</ion-content>
src/app/tabs-routing.module.ts
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { TabsPage } from './tabs.page';
const routes: Routes = [
{
path: 'tabs',
component: TabsPage,
children: [
{
path: 'add-user',
loadChildren: () => import('../add-user/add-user.module').then( m => m.AddUserPageModule)
},
{
path: 'find-enterprises',
loadChildren: () => import('../find-enterprises/find-enterprises.module').then( m => m.FindEnterprisesPageModule)
},
{
path: 'find-applications',
loadChildren: () => import('../find-applications/find-applications.module').then( m => m.FindApplicationsPageModule)
},
{
path: '',
redirectTo: '/tabs/add-user',
pathMatch: 'full'
}
]
},
{
path: '',
redirectTo: '/tabs/add-user',
pathMatch: 'full'
}
];
@NgModule({
imports: [RouterModule.forChild(routes)],
})
export class TabsPageRoutingModule {}
src/app/tabs-page.html
<ion-tabs>
<ion-tab-bar slot="bottom">
<ion-tab-button tab="add-user">
<ion-icon name="people-outline"></ion-icon>
<ion-label>Users</ion-label>
</ion-tab-button>
<ion-tab-button tab="find-enterprises">
<ion-icon name="ellipse"></ion-icon>
<ion-label>Enterprises</ion-label>
</ion-tab-button>
<ion-tab-button tab="find-applications">
<ion-icon name="square"></ion-icon>
<ion-label>Applications</ion-label>
</ion-tab-button>
</ion-tab-bar>
</ion-tabs>
Add the AppRoutingModule
In Angular, the best practice is to load and configure the router in a separate, top-level module that is dedicated to routing and imported by the root AppModule.
By convention, the module class name is AppRoutingModule and it belongs in the app-routing.module.ts in the src/app folder.
src/app/app-routing.module.ts
import { NgModule } from '@angular/core';
import { PreloadAllModules, RouterModule, Routes } from '@angular/router';
const routes: Routes = [
{
path: '',
loadChildren: () => import('./tabs/tabs.module').then(m => m.TabsPageModule)
},
{
path: 'add-enterprise',
loadChildren: () => import('./add-enterprise/add-enterprise.module').then( m => m.AddEnterprisePageModule)
},
{
path: 'add-application/:id',
loadChildren: () => import('./add-application/add-application.module').then( m => m.AddApplicationPageModule)
},
{
path: 'find-enterprise/:id',
loadChildren: () => import('./find-enterprise/find-enterprise.module').then( m => m.FindEnterprisePageModule)
},
{
path: 'find-application/:id',
loadChildren: () => import('./find-application/find-application.module').then( m => m.FindApplicationPageModule)
},
];
@NgModule({
imports: [
RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules })
],
exports: [RouterModule]
})
export class AppRoutingModule { }
NgModules and JavaScript modules
The NgModule system is different from and unrelated to the JavaScript (ES2015) module system for managing collections of JavaScript objects. These are complementary module systems that you can use together to write your apps.
In JavaScript each file is a module and all objects defined in the file belong to that module. The module declares some objects to be public by marking them with the export key word. Other JavaScript modules use import statements to access public objects from other modules.
src/app/app-module.ts
import { CUSTOM_ELEMENTS_SCHEMA, NgModule, NO_ERRORS_SCHEMA } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouteReuseStrategy } from '@angular/router';
import { IonicModule, IonicRouteStrategy } from '@ionic/angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';
import { HttpClientModule } from '@angular/common/http';
import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';
import { FormsModule } from '@angular/forms';
@NgModule({
declarations: [AppComponent],
entryComponents: [],
imports: [
BrowserModule,
IonicModule.forRoot(),
AppRoutingModule,
FormsModule,
HttpClientModule,
],
providers: [
StatusBar,
SplashScreen,
{ provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
],
schemas: [
CUSTOM_ELEMENTS_SCHEMA,
NO_ERRORS_SCHEMA
],
bootstrap: [AppComponent]
})
export class AppModule { }
Conclusion
Now we have an overview of Spring Boot CRUD example when building a CRUD App.
We also take a look at client-server architecture for REST API using Spring Web MVC & Spring Data JPA, as well, we ware creating an Ionic app with Angular 10 project structure for building a front-end app to make HTTP requests and consume responses.