简体   繁体   English

我试图让 ionic 2 应用程序的用户删除列表项。 使用 Angular 使用 Firebase 构建

[英]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,
    });
  }

Firebase Image Firebase 图像

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.

相关问题 我在菜单项上使用反应钩子。 我的第一次点击没有做任何事情,后续点击按预期工作 - I am using react hooks on menu item. My first click does not do anything, subsequent clicks work as intended 我正在尝试使用 Vanilla JavaScript 从 TODO-LIST 应用程序中删除项目 - I am trying to delete Items from in a TODO-LIST App using Vanilla JavaScript ReactJS/Firebase:我如何让用户删除自己? - ReactJS/Firebase : How do I let a user delete themselves? 在reactjs中,我试图有一个按钮让用户保持登录状态 - in reactjs I am trying to have a button let user stay logged in 我正在尝试使用 mysql javascript API 删除数据库中的一条记录 - I am trying to delete a record in the database using mysql javascript API 为什么在尝试从Angular 2应用程序连接和查询Firebase DB时收到此错误消息? - Why am I obtaining this error message trying to connect and query a Firebase DB from an Angular 2 application? 我正在尝试使用Java脚本在click事件上存储列表项,并将其访问到另一页以进行SQL查询,但是我无法 - i am trying to store list item on click event using java script and acces it to another page for sql query, but i cant 我正在尝试使用它来增加 count 变量,但是 let 造成了问题,而 var 工作正常 - I am trying to increase the count variable using this, but let is creating problem , whereas var works fine 我正在尝试使用angular js从输入中调用文本 - I am trying to call text from input using angular js 我正在尝试使用angular显示JSON数组 - i am trying to show JSON array using angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM