How to Develop Basic Firebase CRUD Operations in Angular
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.
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 {
// ...
}
updateTodo(todo: any, newValue: string): void {
// ...
}
}
Also Read:-How to Develop Firebase Authentication in Angular
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';
@NgModule({
declarations: [
AppComponent
],
imports: [
// ...
AngularFireModule.initializeApp(environment.firebase),
AngularFireAuthModule
],
providers: [AngularFireDatabase],
bootstrap: [AppComponent]
})
export class AppModule { }
Reading Todos
imply 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<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', 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 somewhat extraordinary in light of the fact that you'll require a reference to the todo thing that will be updateed or deleted. This should be possible with AngularFireDatabase.object. Articles inside a Firebase have a one of a kind key accessible 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 });
}
Deleting Todos
Deleting an item is just as easy as updating it:
deleteTodo(todo: any): void {
this.af.object('/todos/' + todo.$key).remove();
}
Also Read:-How to Develop Firebase Authentication in Angular