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.
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 {
// ...
}
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';
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>
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 });
}
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:
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 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.
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.
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!
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.