2

I need to delete an entry from firebase using angularjs.the problem is that if I use the index then it deletes all the entries from the firebase, and if I use the key method it does nothing. Here is the code for controller.It is supposed to take key from the firebase from one of the partials.

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

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

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

This is the function to delete an entry from the firebase

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

Here is the code for partial

        <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>         
0

3 Answers 3

3

Remove does not take a key as a parameter.

You need to nest by calling .child(key), and then call remove.

ref.child(key).remove();
Sign up to request clarification or add additional context in comments.

Comments

1

Since your ref is the root of your firebase database, so you need to find one child and remove that child as below

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

Refer more here https://www.firebase.com/docs/web/libraries/angular/api.html#angularfire-firebaseobject-remove

Comments

1

You could also use firebase method called $remove so like this:

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

Docs about remove located here : https://www.firebase.com/docs/web/libraries/angular/guide/synchronized-objects.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.