Tutorial

Basic Firebase CRUD Operations in Angular With AngularFire2

Published on June 8, 2017
author

Alligator.io

Basic 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.

We’ll be using Todos in the post for our examples. Each todo has a content string value for the todo description and a done boolean to indicate if the todo item is completed.

Setup

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

app.component.ts
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 {
    // ...
  }

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

app.module.ts
// ...

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

Reading Todos

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

todos$: AngularFireList;

ngOnInit() {
  this.todos$ = this.af.list('/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$ = this.af.list('/todos', ref => 
  ref.limitToFirst(3)
);

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

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

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 {
  this.af.object('/todos/' + todo.$key)
    .update({ content: todo.content, done: !todo.done });
}

Or update its content:

updateTodo2(todo: any, newValue: string): void {
  this.af.object('/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 {
  this.af.object('/todos/' + todo.$key).remove();
}

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

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Alligator.io

author

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Leave a comment


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more