我开发了简单的 Angular-Firebase 应用程序,它提供基本的 CRUD 功能。
Firebase 中的 json 格式
{
"-J0wuZ_J8P1EO5g4Xfw6" : {
"contact" : "56231545",
"company" : "info",
"city" : "limbdi",
"name" : "priya"
},
"-J0wrhrtgFvIdyMcSL0x" : {
"contact" : "65325422",
"company" : "rilance",
"city" : "jamnagar",
"name" : "pihu"
}
}
用于列出 html 页面中所有数据的 Angular 代码
<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);
};
}
]);
此警报框包含当前员工的姓名。我想删除当前行的员工。但我不知道如何使用 firebase 的 remove()
方法。我访问了 firebase 的文档,所以我得到了运行良好的以下代码。
var current = new Firebase('https://myurl/employee/-J48go0dwY5M3jAC34Op');
current.onDisconnect().remove();
但我想动态地创建它,那么如何获取当前节点的父ID,例如-J48go0dwY5M3jAC34Op
?
请帮我解决一个小问题。
最佳答案
您可以将 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);
}
关于angularjs - 如何使用 Angular 删除firebase中的json数据对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18954639/