从火力地堡删除项目 [英] Remove Item from Firebase

查看:166
本文介绍了从火力地堡删除项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解如何从我的火力地堡中删除项目。我已经成立了一个函数( createProvider )来创建一个项目,但无法弄清楚如何去删除的项目。

I'm trying to understand how to remove an item from my Firebase. I've set up a function (createProvider) to create an item , but can't figure out how to go about removing an item.

HTML

<form role="form" name="createProviderForm">
    <input type="text" ng-model="title">
    <button type="submit" ng-click="createProvider()">Submit</button>
</form>

<div ng-repeat="provider in providers">
    <h3>{{ provider.title }}</h3>
    <button type="button" ng-click="removeProvider()">Remove</button>
  </div>
</div>

JS

var rootRef = new Firebase(FBURL);
var providersRef = rootRef.child('providers');

$scope.newProvider = {};
$scope.providers = [];

providersRef.on('child_added', function(snapshot) {
  $timeout(function() {
    var snapshotVal = snapshot.val();
    console.log(snapshotVal);
    $scope.providers.push({
      title: snapshotVal.title
    });
  });
});

$scope.createProvider = function() {
  var newProvider = {
    title: $scope.title
  };
  providersRef.push(newProvider);
};

$scope.removeProvider = function() {

};

我得尽可能创造一个调​​用的函数 removeProvider ,但不能制定出里面放什么。我知道我必须在某种程度上针对特定项目,然后从列表中删除。我只是不知道如何。

I've got as far as creating a function called removeProvider but can't work out what to put inside it. I realise I've got to somehow target the particular item and then remove it from the list. I'm just not sure how.

任何帮助,这将是AP preciated。在此先感谢!

Any help with this would be appreciated. Thanks in advance!

推荐答案

要从火力地堡删除一个项目,你需要知道它的名称(),这是自动为您生成,当你调用推()来一个新的项目添加到火力地堡。

To remove an item from Firebase, you will need to know its name(), which is automatically generated for you when you call push() to add a new item to Firebase.

所以,你需要将名称绑定到范围:

So you will need to bind the name to the scope:

$scope.providers.push({
  name: snapshot.name(),
  title: snapshotVal.title
});

您再通这个名字进入您的来电 removeProvider 从你的HTML:

You then pass this name into your call to removeProvider from your HTML:

<div ng-repeat="provider in providers">
    <h3>{{ provider.title }}</h3>
    <button type="button" ng-click="removeProvider(provider.name)">Remove</button>
</div>

和用它打电话删除的火力地堡:

And use it to call remove on Firebase:

$scope.removeProvider = function(name) {
  providersRef.child(name).remove();
};

由于@SharpieOne已经评论,这和其他许多东西会自动为您处理,如果你使用AngularFire库(在这种情况下,特别是其 $ asArray()方法)。

这篇关于从火力地堡删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆