javascript - Delete Data from Firebase server Using key or index method - Stack Overflow

I need to delete an entry from firebase using angularjs.the problem is that if I use the index then it

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("/");

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>         

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./");

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>         
Share Improve this question edited Jan 16, 2016 at 20:45 David East 32.6k6 gold badges69 silver badges83 bronze badges asked Jan 16, 2016 at 19:50 Saqib SSaqib S 5415 silver badges22 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 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();

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./docs/web/libraries/angular/api.html#angularfire-firebaseobject-remove

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

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744929680a4601655.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信