4

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.

2 Answers 2

12

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);
}
Sign up to request clarification or add additional context in comments.

7 Comments

hii. i tried your code but its not working because id represent index of item like (0,1,2,3) but in my case i need to pass -J48go0dwY5M3jAC34Op this string after url then only i can able to delete particular json object. so can you tell me how to get this string ?
I added a second sample that directly gets at the id. Give that a shot.
Thanks dear, its working.. :) can you suggest me good tutorial to learn angular-firebase?
sadly, no. I've been going the trial and error route. I now see the top example I gave is for using angularFire() and the bottom for angularFireCollection(). In the angularFireCollection case, list.remove(item) also works.
ohh, is it. anyway o.$id is working totally perfect. it also help me to update json object. may i have ur twitter or gplus id?
|
1

If it's a firebaseArray, you can also do it this way:

$scope.deleteItem = function(item){
 employee.$remove(item);
}

Just pass the item you want to remove from the object.

Note: It won't work for firebaseObjects.

Useful sources:

https://www.firebase.com/docs/web/libraries/angular/api.html#angularfire-firebasearray-removerecordorindex

https://gist.github.com/anantn/4325082

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.