Firebase CRUD Operations in Angular With AngularFire2


We’ve covered Firebase authentication and deploying to Firebase hosting. Now let’s explore how to get started with the realtime database in Angular 2+ apps using the AngularFire2 library. Firebase makes it very easy to get up and running very quickly with populating and performing operations on the database.


First you’ll want to import AngularFireDatabase and AngularFireList as well as inject the former in your constructor:


import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs/Observable';

import { AngularFireDatabase, AngularFireList }
  from 'angularfire2/database';

@Component({ ... })
export class AppComponent implements OnInit {
  todos$: AngularFireList<any[]>;

  constructor(private af: AngularFireDatabase) {}

  ngOnInit() {
    // ...

  addTodo(value: string): void {
    // ...
  deleteTodo(todo: any): void {
    // ...

  toggleDone(todo: any): void {
    // ...

  updateTodo(todo: any, newValue: string): void {
    // ...

You’ll also want to make sure that AngularFireDatabase is provided in your app module:


// ...

import { AngularFireModule } from 'angularfire2';
import { environment } from '../environments/environment';
import { AngularFireDatabase } from 'angularfire2/database';

  declarations: [
  imports: [
    // ...
  providers: [AngularFireDatabase],
  bootstrap: [AppComponent]
export class AppModule { }

Reading Todos

Simply declare a class property of type AngularFireList and get the /todosnode from your Firebase database with AngularFireDatabase.list in the OnInit lifecycle hook:

todos$: AngularFireList<any[]>;

ngOnInit() {
  this.todos$ ='/todos');

The returned observable will contain all the todos that are under /todos in your database. You can also pass-in an object as the second argument to provide querying or filtering options. Let’s say we only want the first 3 todo items:

this.todos$ ='/todos', ref => 

You can unwrap the observable to display the todo items in your template using the async pipe like this:

  <li *ngFor="let todo of (todos$ | async)" [class.done]="todo.done">
    {{ todo.content }}

Creating Todos

Adding a new todo item is really easy, just call push on your AngularFireList instance:

addTodo(value: string): void {
  this.todos$.push({ content: value, done: false });

Updating and Deleting Todos

To update or delete a todo, things are a bit different because you’ll need a reference to the todo item that is to be updated or deleted. This can be done with AngularFireDatabase.object. Objects inside a Firebase have a unique key available as $key:

Updating Todos

Here’s how you would toggle the completed state of a todo:

toggleDone(todo: any): void {'/todos/' + todo.$key)
    .update({ content: todo.content, done: !todo.done });

Or update its content:

updateTodo2(todo: any, newValue: string): void {'/todos/' + todo.$key)
    .update({ content: newValue, done: todo.done });    

As an alternative to the update method on a Firebase object, you could also use set. Using the set method will create a new item if one doesn’t exist and replaces to whole object, so its destructive.

Deleting Todos

Deleting an item is just as easy as updating it:

deleteTodo(todo: any): void {'/todos/' + todo.$key).remove();

Note that setupdate and remove return a promise to you can chain then/catch calls to let you act on a successful operation or an error.

Best resources to learn Angular, Firebase

Leave a Reply

Your email address will not be published. Required fields are marked *