[英]I am trying to let a user of an ionic 2 application delete a list item. Built using Firebase using Angular
The below code shows my create a list method, the HTML and the delete method.下面的代码显示了我的创建列表方法、HTML 和删除方法。 I am struggling to get the delete method working.我正在努力使删除方法起作用。 I get the error 'remove is undefined'?我收到错误“删除未定义”? I am not sure If I have to add the $key in the delete method?我不确定是否必须在删除方法中添加 $key?
//Delete method, this is the method I am struggling. i want a user to be able to delete the list item on slide
deleteList(list) {
list.remove(list);
}
//HTML
<ion-list>
<ion-item-sliding *ngFor="let list of list" >
<ion-item>
<p>An item:
<strong>{{list.list1}}</strong>
</p>
<p>Oooooh is this important:
<strong>{{list.list2}}</strong>
</p>
<p>Another item:
<strong>{{list.list3}}</strong>
</p>
</ion-item>
<ion-item-options side="left">
<button ion-button color="danger" (click)="deleteList(list.$key)">
<ion-icon name="md-trash">Delete</ion-icon>
</button>
</ion-item-options>
</ion-item-sliding>
</ion-list>
//Create list
firebase.auth().onAuthStateChanged(user => {
if (user) {
this.listRef = firebase
.database()
.ref(`/userProfile/${user.uid}/List`);
}
});
}
createList(
list1: string,
list2: string,
list3: string
): ThenableReference {
return this.listRef.push({
list1: list1,
list2: list2,
list3: list3,
});
}
You have some mistakes in your code.You probably wanted to use *ngFor="let list of lists" // lists is the array or collection on which you want to use the loop and list is just a reference(which you could use anything) of the items present inside the lists collection您的代码中有一些错误。您可能想使用 *ngFor="let list of lists" // 列表是您要在其上使用循环的数组或集合,而列表只是一个引用(您可以使用任何) 列表集合中存在的项目
Second mistake is that inside the delete function you should use the reference you are using to reference you firebase database in your case you should have used this.listRef not list第二个错误是,在删除函数中,您应该使用您用来引用您的 firebase 数据库的引用,在您的情况下,您应该使用 this.listRef 而不是 list
for deleting you could use something like this(assuming you have keys inside your list(firebase keys))删除你可以使用这样的东西(假设你的列表中有键(firebase键))
<ion-item-options side="left">
<button ion-button color="danger" (click)="deleteList(list.$key)">
<ion-icon name="md-trash">Delete</ion-icon>
</button>
</ion-item-options>
It is always advisable to use service for these operations as you will have all your firebase related operations and ref at one central place and you could easily use it from any page.始终建议为这些操作使用服务,因为您将在一个中心位置拥有所有与 Firebase 相关的操作和参考,并且您可以从任何页面轻松使用它。
The simplest way to delete data is to call remove() on a reference to the location of that data(from firebase docs) so you can use this to delete the selected item from the firebase storage:删除数据的最简单方法是调用 remove() 对该数据位置的引用(来自 firebase docs),以便您可以使用它从 firebase 存储中删除所选项目:
you can get the authenticated user or current logged in user from:您可以从以下位置获取经过身份验证的用户或当前登录的用户:
import { User } from 'firebase/app'; // import user from firebase
authenticatedUser: User; //declare variable authenticatedUser as type User
firebase.auth().onAuthStateChanged(user => {
if (user) {
this.authenticatedUser = user;
}
then delete:然后删除:
deleteList(key) {
firebase
.database()
.ref(`/userProfile/${this.authenticatedUser.uid}/List/${key}`).remove()
}
First thing is you write wrong *ngFor="let list of list"
It's written like this *ngFor="let list of lists"
.首先是你写错了*ngFor="let list of list"
它是这样写的*ngFor="let list of lists"
。 Remove method also not there so I give and example: Remove 方法也不在那里,所以我举个例子:
<ion-list>
<ion-item-sliding *ngFor="let item of shoppingItems | async">
<ion-item>
{{ item.$value }}
</ion-item>
<ion-item-options side="right">
<button ion-button color="danger" icon-only (click)="removeItem(item.$key)"><ion-icon name="trash"></ion-icon></button>
</ion-item-options>
</ion-item-sliding>
On typescript only Write That在打字稿上只写那个
First Make Privoder and Import It.首先制作Privoder并导入它。
import { DatabaseserviceProvider } from './../../providers/databaseservice/databaseservice';
constructor(public firebaseProvider: DatabaseserviceProvider)
removeItem(id) {
this.firebaseProvider.removeItem(id);
}
databaseservice.ts数据库服务.ts
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
import { AngularFireDatabase, FirebaseListObservable } from 'angularfire2/database';
// import { AngularFire, FirebaseObjectObservable, AngularFireAuth, FirebaseListObservable } from 'angularfire2';
@Injectable()
export class DatabaseserviceProvider {
constructor (
public http: Http,
// private _af: AngularFire,
public afd: AngularFireDatabase) {
console.log('Hello Database serviceProvider Provider');
}
getShoppingItems() {
return this.afd.list('/shoppingItems/');
}
addItem(name) {
this.afd.list('/shoppingItems/').push(name);
}
removeItem(id) {
this.afd.list('/shoppingItems/').remove(id);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.