使用键或索引方法从 Firebase 服务器删除数据

Delete Data from Firebase server Using key or index method

我需要使用 angularjs.the 从 firebase 中删除一个条目问题是,如果我使用索引,它会从 firebase 中删除所有条目,如果我使用 key 方法,它什么也不做。这是 controller.It 的代码应该从其中一个部分的 firebase 中获取密钥。

$scope.deleteContact = function(key){
              ContactList.destroy(key);
              deleteAlert.show();
                        };

contactFactory.factory('ContactList',   function($firebaseObject,$firebaseArray){

var contact = new Firebase("https://mycontactmanager.firebaseio.com/");

这是从 firebase 中删除条目的函数

destroy: function(key){
contact.remove(key);
                      }

这是部分代码

        <td>{{contactItem.name}}</td>
        <td>{{contactItem.email}}</td>
        <td>{{contactItem.phone}}</td>
        <td><a href="#/contact/{{key}}" class="btn btn-success btn-xs">View Contact</a>
        <button class="btn btn-danger btn-xs col-sm-offset-1" ng-click="deleteContact(key)\">Delete</button>


      </td>
      </tr>

      </tbody>         

删除不将键作为参数。

需要先调用.child(key)嵌套,再调用remove.

ref.child(key).remove();

您也可以像这样使用名为 $remove 的 firebase 方法:

ref.child(键).$remove();

有关删除的文档位于此处:https://www.firebase.com/docs/web/libraries/angular/guide/synchronized-objects.html

由于您的 ref 是您的 firebase 数据库的根目录,因此您需要找到一个 child 并将其删除 child,如下所示

ref.child(key).$remove().then(function() {
    // Code after remove
});

在此处参考更多内容https://www.firebase.com/docs/web/libraries/angular/api.html#angularfire-firebaseobject-remove