我想使用javascript从firebase中删除元素

时间:2016-06-04 21:36:04

标签: javascript angularjs firebase

我正在尝试了解如何从Firebase中删除项目。我已经设置了一个函数(saveEmployee)来创建一个项目,但是无法弄清楚如何删除一个项目。

HTML

<tbody ng-repeat="employee in employees"> 
            <tr>
                <td>{{employee.employeeName}}</td>
                <td>{{employee.employeeAge}}</td>
                <td><a class="btn btn-danger" ng-click="removeEmployee(employee.employeeName)" >Delete</a></td>
            </tr>
        </tbody>

JS

$scope.removeEmployee = function(employeeName) {
        console.log(employeeName);
        $scope.myData.child(employeeName).remove();
    };

1 个答案:

答案 0 :(得分:1)

使用Firebase.set()并传入null。这将删除该员工。

$scope.removeEmployee = function(employeeId) {
   var employeeRef = new Firebase('https://myfirebaseurl.firebaseio.com/employees/' + employeeId);
   employeeRef.set(null);
};

或使用回调处理程序。

$scope.removeEmployee = function(employeeId) {
   var employeeRef = new Firebase('https://myfirebaseurl.firebaseio.com/employees/' + employeeId);
   employeeRef.set(null, function(error) {
       if (error) {
           console.log(error);
       } else {
           console.log('Employee deleted.');
       }
   });
};