Skip to content

Instantly share code, notes, and snippets.

@kobvel
Created October 27, 2017 15:31
Show Gist options
  • Save kobvel/b148e94f669ab2fb06714e0502f70778 to your computer and use it in GitHub Desktop.
Save kobvel/b148e94f669ab2fb06714e0502f70778 to your computer and use it in GitHub Desktop.
import { Component } from '@angular/core';
import { AngularFireDatabase, AngularFireList } from 'angularfire2/database';
import { Observable } from 'rxjs/Observable';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
itemsRef: AngularFireList<any>;
items: Observable<any[]>;
constructor(db: AngularFireDatabase) {
this.itemsRef = db.list('messages');
this.items = this.itemsRef.snapshotChanges().map(changes => {
return changes.map(c => ({ key: c.payload.key, ...c.payload.val() }));
});
}
addItem(newName: string) {
this.itemsRef.push({ text: newName });
}
updateItem(key: string, newText: string) {
this.itemsRef.update(key, { text: newText });
}
deleteItem(key: string) {
this.itemsRef.remove(key);
}
deleteEverything() {
this.itemsRef.remove();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment