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 FirebaseListObservable 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, FirebaseListObservable } from 'angularfire2/database'; @Component({ ... }) export class AppComponent implements OnInit { todos$: FirebaseListObservable<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:
app.module.ts
// ... import { AngularFireModule } from 'angularfire2'; import { environment } from '../environments/environment'; import { AngularFireDatabase } from 'angularfire2/database'; @NgModule({ declarations: [ AppComponent ], imports: [ // ... AngularFireModule.initializeApp(environment.firebase), AngularFireAuthModule ], providers: [AngularFireDatabase], bootstrap: [AppComponent] }) export class AppModule { }
Reading Todos
Simply declare a class property of type FirebaseListObservable and get the /todos node from your Firebase database with AngularFireDatabase.list
in the OnInit lifecycle hook:
todos$: FirebaseListObservable<any[]>; 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', { query: { 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 FirebaseListObservable 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.