Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to delete json data object in firebase using angular?

I have developed simple angular-firebase application which provide basic CRUD functionality.

json format in firebase

{
  "-J0wuZ_J8P1EO5g4Xfw6" : {
    "contact" : "56231545",
    "company" : "info",
    "city" : "limbdi",
    "name" : "priya"
  },
  "-J0wrhrtgFvIdyMcSL0x" : {
    "contact" : "65325422",
    "company" : "rilance",
    "city" : "jamnagar",
    "name" : "pihu"
  }
}

angular-code for listing all data in html page

<table class='table table-hover'>
    <tr>
        <th>Name</th>
        <th>City</th>
        <th>Company</th>
        <th>Contact</th>
        <th></th>
    </tr>

    <tr ng-repeat="item in employee">
        <td>{{item.name}}</td>
        <td>{{item.city}}</td>
        <td>{{item.company}}</td>
        <td>{{item.contact}}</td>
        <td><button class='btn btn-warning btn-mini' ng-click='delemp(employee[$index])'>X</button></td>
    </tr>
</table>

when someone click on button it fire delemp function which take employee's current index as argument.

var myapp = angular.module('myapp',['firebase']);
myapp.controller('MyCtrl', ['$scope', 'angularFireCollection',
  function MyCtrl($scope, angularFireCollection) {

       $scope.delemp=function($current_emp){
          alert($current_emp.name);
    };
  }
]);

this alert box contain current employee's name. I want to delete current row of employee. but I don't know how to use remove() method of firebase. I have visited documentation of firebase so I got bellow code which is working nice.

var current = new Firebase('https://myurl/employee/-J48go0dwY5M3jAC34Op');
        current.onDisconnect().remove();

but I want to make it dynamically so how can I get parent id of current node like -J48go0dwY5M3jAC34Op ?

please help me to figure out small issue.

like image 251
Jaydipsinh Avatar asked Dec 11 '22 11:12

Jaydipsinh


1 Answers

instead of passing the object, you could just pass the id into your delete function.

<li ng-repeat="(key,item) in list">
  <button ng-click="deleteItem(key)">delete</button> {{item.name}} 
</li>

$scope.deleteItem = function(id){
  var itemRef = new Firebase(url + '/' + id);
  itemRef.remove();
}

edit: this also works

<div ng-repeat="item in list">
    <button ng-click="writeID(item)">log id</button>{{item.$id}} {{item}}<hr>
</div>

$scope.writeID = function(o){
  console.log(o.$id);
}
like image 157
Jeff Ilse Avatar answered Jan 19 '23 15:01

Jeff Ilse