This is a step by step tutorial on how to implement Angular 5 CRUD Operations with Firebase project. So let’s demonstrate Create Read Update and Delete Operations in an Angular 5 project. In one of my other article we discussed on CRUD Operations in Angular 5 with Web API.

Application Demo :

angular 5 crud operation project demo

Following tools and modules are used for this project :

– Angular CLI
– Angular 5
– Firebase project
– Firebase and AngularFire2 (npm packages)
– ngx-Toastr  (npm package)
– Bootstrap and Font-Awesome Icons
– VS Code Editor

we assume that you have installed required packages and software for angular 5 development.

Create Angular 5 Application

In-order to create an Angular 5 application, you can use following Angular CLI command

Copy to Clipboard

it will create an angular 5 application in the current directory and install some default npm packages.
To run this application
Copy to Clipboard

it will open this project in your default web browser from 4200 port.

Create Firebase Project

Goto Firebase Console. it will automatically log in with your gmail account.

Click On Add Project. it will open a pop-up window, inside that name your project, then click on Add Project button.After creating firebase project, Project Dashboard / Project Overview will be opened like this.

firebase project overview - click on Add Firebase to your web app

Click on ‘Add Firebase to your web app’, it will open another popup window with firebase project configuration. copy config object from there.

Copy Firebase Configuration Details

then past inside environment.ts  file as firebaseConfig.

Copy to Clipboard

Change FirebaseDB Permission

Goto Database > Rules Tab (if not showing click on ‘get started’)

Set following read & write permission

Copy to Clipboard

Install Firebase & Angularfire2 Modules

Firebase and Angularfire2 are official packages to interact with firebase project from angular applications.

In-order to install these packages use following npm commands

Copy to Clipboard

Open appmodule.ts file, then initialize firebase module with firebase project configuration.
Copy to Clipboard

Add Required Angular 5 CRUD Components, Model and Service Classes

Now we need to add 3 components, to add angular components you can use following Angular-CLI commands

Copy to Clipboard

remaining two components will be child components for this employees component.
Copy to Clipboard

Open app.module.ts file, Add newly added components into declarations array.
Copy to Clipboard

Let’s Start the Design

We’ll use Bootstrap and Font-Awesome Icons For Application Design. So first of all add CDN reference for the style sheets inside index.html .

Copy to Clipboard

Update app.component.html as follows
Copy to Clipboard

Add Following to employees.component.html file.
Copy to Clipboard

We need Model and Services to design remaining child components.

Create Service and Model Classes

To create these classes let’s add a new folder with name shared inside employees folder (/src/app/employees/ ).

Now create employee model class

Copy to Clipboard

In this application, we deals with employee details like

  • Full Name
  • Position
  • Office Location
  • Salary

So let’s add properties corresponding to these employee’s details inside employee.model.ts file.

Copy to Clipboard

$key is used to store unique key automatically generated by firebase DB when we insert a new record.

Inside the service file we will do Firebase CRUD Operation as follows

Copy to Clipboard

EmployeeService contains selectedEmployee property of the type employee model class Employee, to store currently active employee details. employeeList variable store all the employee records from Firebase DB.

In-order to interact with firebase project we have injected AngularFireDatabase as firebase. getData function initialize employeeList from firebase node ’employees’. we will store employee records under this node. rest of the functions are self explanatory.

Using selectedEmployee property we can design form for insert and update operation inside employee component,
In-order to list inserted employees we can use employeeList property in employee-list component. So first of all we have to Inject this service class inside child components. Before that we have to discuss our application structure.

Application Structure

Copy to Clipboard

This our application structure, component employees will be the parent component for employee and employee-list component.

We are going to inject EmployeeService inside the Parent Component Employees. and there by we can access the same injected service instance from child components Employee and Employee-List. so whenever we make make changes in one child component same change can be seen from other child component also.

Inject Employee Service in Components :

first of all inject the service class inside parent component Employees

Copy to Clipboard

To inject a class inside a component , mention the class inside component providers array and then create private parameter inside component constructor.

Now we can use this injected instance in child components, for that you can do this, Inside employee.component.ts file

Copy to Clipboard

Inside employee-list.component.ts file
Copy to Clipboard

Angular 5 CRUD Operations Form

We’ll create an employee form to implement Insert and Update Operation with employee Component. For that, let’s create a Template Driven Form(TDF) using selectedEmployee property from injected EmployeeService Class.

So first of all we have to import FormsModule in appmodule.ts file.

Copy to Clipboard

You can use following html in employee.component.html file.
Copy to Clipboard

and this employee form with data looks like this

Form to implement angular 5 CRUD Operations

This form is a collection of label – text box pair with Submit and Reset button. We have an extra hidden field to store $key of current employee. Same form is used for Insert and Update Operation.

Form Validation

required attribute is set to name text box, so employee name is mandatory to submit this form. When name text-box is invalid, ng-invalid and ng-dirty classes are added to it. So based on these we have implemented form validation.

when name is empty, employee form as whole is not valid so we add conditional disable attribute to Submit Button.

Copy to Clipboard

to show validation error indication we’ll show red border around the text box using CSS. Here is the complete css rules for this application
Copy to Clipboard

Insert, Update and Reset Operation

Inside employee.component.ts file we’ll write code for Insert and Update Operation. Before that I’m going to install ngx-toastr from npm package, this package helps us to show notification message inside angular applications.

ngx-toastr installation

In-order to install the package, you can use following npm command.

Copy to Clipboard

then add ToastrModule inside app.module.ts file.
Copy to Clipboard

Open .angular-cli.json, then update styles array.
Copy to Clipboard

Now you can add following code inside employee component.
Copy to Clipboard

restForm() function is used reset form controls value to initial stage, we called this function from reset button click event and from ngOnint  Lifecycle Hook to initialise the form.

Inside the form submit event function OnSubmit, we implement both insert and update operation based on $key value. To show the success message, we use ToastrService class object tostr.

List Inserted Records and Delete Operation

Using employee-list component, we’ll list all inserted employees and then implement Delete Operation.

you can add following inside employee-list component.

Copy to Clipboard

Inside this we have injected EmployeeService and ToastrService Classes. Also we created an Employee array employeeList. Inside the ngOnint  Lifecycle Hook, we subscribe to firebase db collection and we store employee records inside an array. Using this array variable, we will display inserted employees inside employee-list component.

employee-list.component.html looks like this.

Copy to Clipboard

Component design will look like this

show employee records inside an html table

when we click on pencil button, it will call onEdit() function to populate the corresponding record inside the employee form, after changing user’s details,he can update record as we discussed before(inside employee component).Using trash icon we implemented delete operation with onDelete()  function.

Download Project Source Code : https://goo.gl/83tyjH

Step by Step Video Tutorial

Thanks for the visit, hope you liked it, let me know your comments.