11 Ionic App With Firebase
11 Ionic App With Firebase
Database
Source : https://www.positronx.io/set-up-firebase-database-in-ionic-angular/
https://www.positronx.io/build-ionic-firebase-crud-app-with-angular/
SETTING UP FIREBASE WEB APP TO IONIC PROJECT
2. Create a project
3. Once the project has been successfully created, add firebase to your project. Choose
web App.
4. Copy the yellow codes as it will be added to your ionic project later.
5. Click at Realtime Database. Then Create Database.
CREATING AN IONIC PROJECT AND ADDING FIREBASE WEB APP TO IONIC
PROJECT
3. Add firebase config key – you need to get the key from your Firebase Project App in
the environment.ts file.
PLEASE ENSURE THAT YOU COPY THE ENVIRONMENT CONFIG FROM YOUR
FIREBASE.
environment.ts
@NgModule({
declarations: [AppComponent],
entryComponents: [],
imports: [
BrowserModule,
IonicModule.forRoot(),
AppRoutingModule,
AngularFireModule.initializeApp(environment.firebaseConfig),
AngularFireAuthModule,
AngularFirestoreModule,
AngularFireStorageModule,
AngularFireDatabaseModule,
],
providers: [{ provide: RouteReuseStrategy, useClass:
IonicRouteStrategy }],
bootstrap: [AppComponent],
})
export class AppModule {}
5. Create two more pages
ionic generate page make-appointment
ionic generate page edit-appointment
<ion-app>
<ion-router-outlet></ion-router-outlet>
</ion-app>
<ion-tabs>
<ion-tab-bar slot="bottom">
<ion-tab-button routerLinkActive="tab-selected" routerLink="/home"
class="ios" tab="home">
<ion-icon name="list-box"></ion-icon>
<ion-label>Appointments</ion-label>
</ion-tab-button>
<ion-tab-button routerLinkActive="tab-selected" routerLink="/make-
appointment" class="ios" tab="make-appointment">
<ion-icon name="add-circle"></ion-icon>
<ion-label>Book Appointment</ion-label>
</ion-tab-button>
</ion-tab-bar>
</ion-tabs>
7. Create a shared folder in your project. Then, create a new file called
Appointment.ts in that folder.
Appointment.ts
9. Edit appointment.service.ts
appointment.service.ts
To Perform CRUD (Create, Read, Update, Delete) operations, we need to edit the
following files.
1. Create/add Data
make-appointment.module.ts
make-appointment.page.ts
make-appointment.page.html
make-appointment.module.ts
@NgModule({
imports: [
CommonModule,
FormsModule,
IonicModule,
ReactiveFormsModule,
MakeAppointmentPageRoutingModule
],
declarations: [MakeAppointmentPage]
})
export class MakeAppointmentPageModule {}
2. Update make-appointment.page.ts to add functions. Once the form is
submitted, createBooking() will be called to add a new object data into the database.
make-appointment.page.ts
@Component({
selector: 'app-make-appointment',
templateUrl: './make-appointment.page.html',
styleUrls: ['./make-appointment.page.scss'],
})
export class MakeAppointmentPage implements OnInit {
bookingForm: FormGroup;
constructor(
private aptService: AppointmentService,
private router: Router,
public fb: FormBuilder
) { }
ngOnInit() {
this.bookingForm = this.fb.group({
name: [''],
email: [''],
mobile: ['']
});
}
formSubmit() {
if (!this.bookingForm.valid) {
return false;
} else {
this.aptService.createBooking(this.bookingForm.value).then(res =>
{
console.log(res);
this.bookingForm.reset();
this.router.navigate(['/home']);
})
.catch(error => console.log(error));
}
}
}
3. Update make-appointment.page.html
make-appointment.page.html
<ion-header>
<ion-toolbar class="ios hydrated" color="secondary">
<ion-title class="ios title-ios hydrated">Booking</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
<ion-list lines="full">
<form [formGroup]="bookingForm" (ngSubmit)="formSubmit()">
<ion-item>
<ion-label position="floating">Name</ion-label>
<ion-input formControlName="name" type="text" required></ion-
input>
</ion-item>
<ion-item>
<ion-label position="floating">Email</ion-label>
<ion-input formControlName="email" type="text" required>
</ion-input>
</ion-item>
<ion-item>
<ion-label position="floating">Mobile</ion-label>
<ion-input formControlName="mobile" type="text" required>
</ion-input>
</ion-item>
<ion-row>
<ion-col>
<ion-button type="submit" color="primary" shape="full"
expand="block">Book Appointment</ion-button>
</ion-col>
</ion-row>
</form>
</ion-list>
</ion-content>
C RD U
Read the list from the database and Remove a data from
the list.
1. To display the list of appointment and to delete the booking, edit home.page.ts
home.page.ts
<ion-header>
<ion-toolbar color="secondary">
<ion-title>
Dr. Mashi's Consultation
</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
<ion-list class="ios list-ios hydrated">
<ion-list-header class="ios hydrated">
Appointments
</ion-list-header>
<ion-item *ngFor="let booking of Bookings" class="user-list">
<ion-label>
<h5>
<ion-icon name="person"></ion-icon> {{booking.name}}
</h5>
<p>
<ion-icon name="call"></ion-icon> {{booking.mobile}}
</p>
</ion-label>
<div class="item-note" item-end>
<button ion-button clear [routerLink]="['/edit-appointment/',
booking.$key]">
<ion-icon name="create" style="zoom:2.0"></ion-icon>
</button>
<button ion-button clear (click)="deleteBooking(booking.$key)">
<ion-icon name="trash" style="zoom:2.0"></ion-icon>
</button>
</div>
</ion-item>
</ion-list>
CR U D
Update the existing data
app-routing.module.ts
2. Edit edit-appointment-module.ts to include the reactiveform.
edit-appointment.module.ts
@NgModule({
imports: [
CommonModule,
FormsModule,
IonicModule,
ReactiveFormsModule,
EditAppointmentPageRoutingModule
],
declarations: [EditAppointmentPage]
})
export class EditAppointmentPageModule {}
edit-appointment.page.ts
edit-appointment.page.html
<ion-header>
<ion-toolbar color="secondary">
<ion-title>
Edit Appointment
</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
<ion-list lines="full">
<form [formGroup]="updateBookingForm" (ngSubmit)="updateForm()">
<ion-item>
<ion-label position="floating">Name</ion-label>
<ion-input formControlName="name" type="text" required></ion-
input>
</ion-item>
<ion-item>
<ion-label position="floating">Email</ion-label>
<ion-input formControlName="email" type="text" required>
</ion-input>
</ion-item>
<ion-item>
<ion-label position="floating">Mobile</ion-label>
<ion-input formControlName="mobile" type="text" required>
</ion-input>
</ion-item>
<ion-row>
<ion-col>
<ion-button type="submit" color="primary" shape="full"
expand="block">Update Appointment</ion-button>
</ion-col>
</ion-row>
</form>
</ion-list>
</ion-content>
That’s all