I've been searching extensively for a solution to this problem. Despite following the documentation on AngularFire 2 and Angular 2, I am unable to find a working answer. My goal is simply to delete a specific entry in my Firebase database using its unique key. Although I understand it can be done in Angular 4, I need to accomplish this task in Angular 2.
Any assistance provided would be immensely appreciated!
import { Component, OnInit } from '@angular/core';
import { AngularFireDatabase, FirebaseObjectObservable } from 'angularfire2/database';
import {Observable} from "rxjs/Observable";
@Component({
selector: 'app-listings',
templateUrl: './listings.component.html',
styleUrls: ['./listings.component.scss']
})
export class ListingsComponent implements OnInit {
items: Observable<any[]>;
item: FirebaseObjectObservable<any[]>;
constructor(db: AngularFireDatabase) {
this.items = db.list('Harry');
this.item = db.object('/Harry/');
}
delete() {
this.item.remove(); // removes all data.
}
ngOnInit() {
}
}
EDIT: Added the HTML
<ul class="list-group">
<li class="list-group-item" *ngFor="let item of items | async">
<p><strong>Name: </strong>{{item.name}}</p>
<p><strong>Email: </strong>{{item.email}}</p>
<p><strong>Mobile: </strong>{{item.mobile}}</p>
<p><strong>Address: </strong>{{item.address}}</p>
<div class="editDel row">
<div class="col-sm-1"><button class="btn-primary">Edit</button></div>
<div class="col-sm-1"><button class="btn-danger" (click)="delete()">Delete</button></div>
</div>
</li>
</ul>
EDIT: Added package versions
{
"name": "ng2-firebase-addresslist",
"version": "0.0.0",
"license": "MIT",
"angular-cli": {},
"scripts": {
"ng": "ng",
"start": "ng serve",
"test": "ng test",
"pree2e": "webdriver-manager update --standalone false --gecko false",
"e2e": "protractor"
},
"private": true,
"dependencies": {
"@angular/common": "^2.3.1",
"@angular/compiler": "^2.3.1",
"@angular/core": "^2.3.1",
"@angular/forms": "^2.3.1",
"@angular/http": "^2.3.1",
"@angular/platform-browser": "^2.3.1",
"@angular/platform-browser-dynamic": "^2.3.1",
"@angular/router": "^3.3.1",
"angular2-flash-messages": "^1.0.8",
"angularfire2": "^2.0.0-beta.7-pre",
"core-js": "^2.4.1",
"firebase": "^3.9.0",
"rxjs": "^5.0.1",
"ts-helpers": "^1.1.1",
"zone.js": "^0.7.2"
},
"devDependencies": {
"@angular/cli": "^1.4.4",
"@angular/compiler-cli": "^2.3.1",
"@types/jasmine": "2.5.38",
"@types/node": "^6.0.42",
"codelyzer": "~2.0.0-beta.1",
"jasmine-core": "2.5.2",
"jasmine-spec-reporter": "2.5.0",
"karma": "1.2.0",
"karma-chrome-launcher": "^2.0.0",
"karma-cli": "^1.0.1",
"karma-jasmine": "^1.0.2",
"karma-remap-istanbul": "^0.2.1",
"protractor": "~4.0.13",
"ts-node": "1.2.1",
"tslint": "^4.3.0",
"typescript": "~2.0.3",
"webpack": "^3.6.0"
}
}
There seems to be a large discrepancy among guides and tutorials regarding this issue mainly due to different versions of AngularFire2 altering its functionality with each release. This inconsistency has been quite challenging for me as a learner.