【问题标题】:how to delete json data object in firebase using angular?如何使用角度删除firebase中的json数据对象?
【发布时间】:2013-09-28 01:51:07
【问题描述】:

我开发了提供基本 CRUD 功能的简单 angular-firebase 应用程序。

firebase 中的 json 格式

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

用于在 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();

但我想动态创建它,所以如何获取当前节点的父 ID,如 -J48go0dwY5M3jAC34Op

请帮我解决小问题。

【问题讨论】:

    标签: angularjs firebase


    【解决方案1】:

    如果是firebaseArray,也可以这样:

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

    只需传递要从对象中删除的项目。

    注意:它不适用于 firebaseObjects。

    有用的资源:

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

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

    【讨论】:

      【解决方案2】:

      您可以将 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);
      }
      

      【讨论】:

      • 嗨。我尝试了您的代码,但它不起作用,因为 id 表示项目的索引,如 (0,1,2,3) 但在我的情况下,我需要在 url 之后传递 -J48go0dwY5M3jAC34Op 这个字符串,然后只有我才能删除特定的 json 对象。那么你能告诉我如何得到这个字符串吗?
      • 我添加了第二个直接获取 id 的样本。试一试。
      • 谢谢亲爱的,它的工作原理.. :) 你能推荐我学习 angular-firebase 的好教程吗?
      • 很遗憾,没有。我一直在尝试和错误的路线。我现在看到我给出的最上面的例子是使用 angularFire() 和底部的 angularFireCollection()。在 angularFireCollection 的情况下, list.remove(item) 也可以。
      • 哦,是吗。无论如何 o.$id 工作得非常完美。它还帮助我更新 json 对象。我可以有你的 twitter 或 gplus id 吗?
      猜你喜欢
      • 1970-01-01
      • 2019-09-12
      • 1970-01-01
      • 1970-01-01
      • 2018-07-26
      • 2021-12-10
      • 1970-01-01
      • 2020-09-02
      • 1970-01-01
      相关资源
      最近更新 更多