angularjs - 从 firebase 中删除项目

标签 angularjs firebase ionic-framework firebase-realtime-database firebase-authentication

我是 Firebase 新手,想知道如何才能从用户数据库中删除项目,并且只有在用户插入特定代码(例如 1234)后该项目才会被删除

订单的 Json 示例

 {
      "address_id" : "-Kn6eXyJ9XhglrVgA3eo",
      "item_qty" : 1,
      "payment_id" : "CREDIT",
      "product_id" : "item1",
      "product_image" : "chicken_maharaja",
      "product_name" : "New Chicken Maharaja",
      "product_price" : 130,
      "status" : "Queued",
      "user_id" : "D6yOQ1ZwWwYL69z80xJ0rLTASwG2",
      "user_name" : "Bogdan Incrosnatu"
    }

显示用户当前订单的 Controller

.controller('lastOrdersCtrl', function($scope,$rootScope,fireBaseData,sharedUtils) {

    $rootScope.extras = true;
    sharedUtils.showLoading();

    //Check if user already logged in
    firebase.auth().onAuthStateChanged(function (user) {
      if (user) {
        $scope.user_info = user;

        fireBaseData.refOrder()
          .orderByChild('user_id')
          .startAt($scope.user_info.uid).endAt($scope.user_info.uid)
          .once('value', function (snapshot) {
            $scope.orders = snapshot.val();
            $scope.$apply();
          });
          sharedUtils.hideLoading();
      }
    });



})

最后订单.html

<ion-view style="" class=" " id="page10" title="Orders">
    <ion-content class="has-header" padding="true">


      <ion-list ng-repeat="item in orders" ng-hide="user.hide">

        <ion-item class="item-thumbnail-left" >

          <img  ng-src="{{'img/fk/'+ item.product_image +'.jpg'}}" >

          <a class="badge badge-balanced" style="position: absolute;right:0;" >{{item.status}}</a>
          <h4> {{item.product_name}} </h4> 

          <button ng-click="removeRecord(user)">Delete</button>



        </ion-item>

      </ion-list>


    </ion-content>
</ion-view>

最佳答案

如果你使用 angularjs 的意思。为什么你不能使用 Angular Fire,它是 Firebase 提供的一个库。只需添加 cdn 并在

中注入(inject) firebase 模块即可
var app=angular.module("appname",["firebase"]) like this

你的主要 Angular js 模块,然后在你的 Controller 中。注入(inject) $firebaseArray(firebase提供的服务)。

app.controller("controllername",function($scope,$firebaseArray){
  firebase.auth().onAuthStateChanged(function (user) {
  if (user) {
    $scope.user_info = user;

   var fb_query= firebase.database().ref("Pass your tree name here")
      .orderByChild('user_id')
      .startAt($scope.user_info.uid).endAt($scope.user_info.uid);

然后将其传递给 firebasearray

$scope.orders=$firebaseArray(fb_query);
$scope.orders.$loaded().then(function(){
    //you will get data here.
 var Rec= $scope.orders.$getRecord(//Pass the Id here)
 //you have to pass record id i.e($id) from the firebase object, to delete a data from firebase tree.
 //for ex
 var Rec= $scope.orders.$getRecord(1);
 if(Rec){
   scope.orders.$remove(rec).then(function(){
   //Success 
}).catch(function(){
   //error..
})
 }
    })  
  }
});
})

//或者做类似的事情。

var object={}
object["/Your tree name"+ pass ur Id]=null;
firebase.database().ref().update(object).then(function(){
  console.log("Successfully deleted")
})

关于angularjs - 从 firebase 中删除项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44805935/

相关文章:

jquery - Bootstrap 文件输入 -Kartik - 与 requirejs 、 AngularJS 一起使用时失败且没有错误消息

angular - 使用 Angular 将文件上传到firebase时如何处理firebase存储中的两个存储桶

ios - Ionic 或 Cordova 可以支持托管应用程序配置吗?

angular - 如何在几秒钟后更改 div 的背景颜色?

ionic-framework - 在 Ionic 2 中,如何创建使用 Ionic 组件的自定义组件?

Javascript 异步跳过 if-else

javascript - 在 ng-include 之后调用 ng-submit

javascript - 我如何在 ng Controller 之间传输数据(使用 localhost link api )

javascript - stripe firebase 函数设置默认付款

ios - 用于 iOS 的 cordova-plugin-firebase