2013-09-23 57 views
4

我開發了一個簡單的角度firebase應用程序,它提供了基本的CRUD功能。在火力如何使用angular刪除firebase中的json數據對象?

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

角碼

JSON格式在HTML頁面中列出所有數據

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

當有人點擊按鈕觸發delemp這需要員工目前的指數作爲參數的功能。

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

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

此警報框包含當前員工的姓名。我想刪除當前的員工行。但我不知道如何使用remove() firebase的方法。我訪問過firebase的文檔,所以我得到了波紋管代碼,它工作的很好。

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

,但我想讓它動態地這樣我怎麼能得到像-J48go0dwY5M3jAC34Op當前節點的父ID?

請幫我弄清楚小問題。

回答

12

而不是傳遞的對象,你可以傳遞給你的刪除函數的id。

<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(); 
} 

編輯: 這也適用

<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); 
} 
+0

HII。我試過你的代碼,但它不工作,因爲id代表像(0,1,2,3)項目的索引,但在我的情況下,我需要傳遞-J48go0dwY5M3jAC34Op這個字符串後url然後只我可以刪除特定的json對象。那麼你能告訴我如何得到這個字符串嗎? – Jaydipsinh

+0

我添加了第二個示例,直接獲取id。給那一槍。 –

+0

謝謝親愛的,它的工作.. :) 你可以建議我學習角-firebase的好教程? – Jaydipsinh

相關問題