Building a CRUD Application with Angular and Firebase

 Building a CRUD Application with Angular and Firebase


Angular is a popular framework for building modern web applications, and Firebase is a powerful backend platform for building web and mobile applications. In this blog post, we'll explore how to build a simple CRUD (Create, Read, Update, Delete) application using Angular and Firebase.


To get started, we'll create a new Angular project using the Angular CLI. Open a terminal and run the following command:


javascript


ng new crud-app

Next, we'll add the AngularFire library to our project. This library provides a set of Angular components and services for interacting with Firebase. Run the following command to install the library:


java


npm install firebase @angular/fire

With the AngularFire library installed, we can now create a simple CRUD application. Let's start by creating a new component for displaying a list of items. Run the following command to create a new component:



ng generate component item-list

Next, let's add the Firebase configuration to our "environment.ts" file and initialize the Firebase app in the "app.module.ts" file. We'll also import the "AngularFireModule" and "AngularFirestoreModule" modules and add them to the "imports" array in "app.module.ts". For example:


javascript


// environment.ts

export const environment = {

  production: false,

  firebase: {

    apiKey: 'YOUR_API_KEY',

    authDomain: 'YOUR_AUTH_DOMAIN',

    databaseURL: 'YOUR_DATABASE_URL',

    projectId: 'YOUR_PROJECT_ID',

    storageBucket: 'YOUR_STORAGE_BUCKET',

    messagingSenderId: 'YOUR_MESSAGING_SENDER_ID',

    appId: 'YOUR_APP_ID',

    measurementId: 'YOUR_MEASUREMENT_ID'

  }

};

python


// app.module.ts

import { NgModule } from '@angular/core';

import { BrowserModule } from '@angular/platform-browser';

import { AngularFireModule } from '@angular/fire';

import { AngularFirestoreModule } from '@angular/fire/firestore';

import { environment } from '../environments/environment';

import { AppComponent } from './app.component';

import { ItemListComponent } from './item-list/item-list.component';


@NgModule({

  declarations: [

    AppComponent,

    ItemListComponent

  ],

  imports: [

    BrowserModule,

    AngularFireModule.initializeApp(environment.firebase),

    AngularFirestoreModule

  ],

  providers: [],

  bootstrap: [AppComponent]

})


export class AppModule { }

In this example, we're importing the "AngularFireModule" and "AngularFirestoreModule" modules and configuring the Firebase app using the "environment.ts" file.


Next, let's update the "item-list.component.ts" file to display a list of items. We'll import the "AngularFirestore" service from AngularFire and use it to query the Firebase database and retrieve a list of items. We'll also update the "item-list.component.html" file to display the list of items. For example:


typescript


// item-list.component.ts

import { Component } from '@angular/core';

import { AngularFirestore } from '@angular/fire/firestore';

import { Observable } from 'rxjs';


@Component({

  selector: 'app-item-list',

  templateUrl: './item-list.component.html',

  styleUrls: ['./item-list.component.css']

})

export class ItemListComponent {

  items: Observable<any[]>;


  constructor(private firestore: AngularFirestore) {

    this.items = firestore.collection('items').valueChanges();

  }

}


php


<!-- item-list.component.html -->

<ul>

  <li *ngFor="let item of items | async">{{ item.name }}</li>

</ul>


In this example, we're using the "AngularFirestore" service to query the "items" collection in the Firebase database and retrieve a list of items. We're also using the "async" pipe in the template to subscribe to the "items" observable and display the list of items.


Now, let's add some functionality to create, update, and delete items. We'll add a form to the "item-list.component.html" file for creating new items, and we'll add buttons for updating and deleting existing items. We'll also update the "item-list.component.ts" file to handle these actions. For example:


php


<!-- item-list.component.html -->

<ul>

  <li *ngFor="let item of items | async">

    {{ item.name }} <button (click)="editItem(item)">Edit</button> <button (click)="deleteItem(item)">Delete</button>

  </li>

</ul>

<form (ngSubmit)="createItem()">

  <input type="text" [(ngModel)]="newItemName" name="newItemName" placeholder="New item name">

  <button type="submit">Add item</button>

</form>


typescript


// item-list.component.ts

import { Component } from '@angular/core';

import { AngularFirestore } from '@angular/fire/firestore';

import { Observable } from 'rxjs';


@Component({

  selector: 'app-item-list',

  templateUrl: './item-list.component.html',

  styleUrls: ['./item-list.component.css']

})

export class ItemListComponent {

  items: Observable<any[]>;

  newItemName: string = '';


  constructor(private firestore: AngularFirestore) {

    this.items = firestore.collection('items').valueChanges();

  }


  createItem() {

    this.firestore.collection('items').add({

      name: this.newItemName

    });

    this.newItemName = '';

  }


  editItem(item) {

    // TODO: Implement edit functionality

  }


  deleteItem(item) {

    this.firestore.collection('items').doc(item.id).delete();

  }

}


In this example, we've added a form for creating new items and buttons for updating and deleting existing items. We've also added functions for creating, editing, and deleting items. The "createItem" function adds a new item to the Firebase database, and the "deleteItem" function deletes an item from the database.


Finally, we can add some styling to our application using the "item-list.component.css" file. For example:


css


/* item-list.component.css */

ul {

  list-style: none;

  margin: 0;

  padding: 0;

}


li {

  display: flex;

  justify-content: space-between;

  align-items: center;

  padding: 10px;

  border-bottom: 1px solid #ccc;

}


li:last-child {

  border-bottom: none;

}


form {

  display: flex;

  align-items: center;

  margin-top: 10px;

}


input {

  flex: 1;

  padding: 10px;

  margin-right: 10px;

}


button {

  padding: 10px;

  background-color: #007bff;

  color: #fff;

  border: none;

  cursor: pointer;

}


button:hover {

  background-color: #0062cc;

}


With these changes, we now have a fully functional CRUD application using Angular and Firebase. We can create, read, update, and delete items from the Firebase database, and our changes are immediately reflected in the UI.

No comments:

Post a Comment

The Importance of Cybersecurity in the Digital Age

 The Importance of Cybersecurity in the Digital Age Introduction: In today's digital age, where technology is deeply intertwined with ev...