javascript - 使用 ionic 和 firebase 检索点击时的特定数据

标签 javascript html firebase ionic-framework firebase-realtime-database

我目前正在尝试使用 AngularFire 学习 ionic 框架和 Firebase。

现在我正在尝试开发一个具有多个用户帐户的简单(或者我认为的)联系人应用程序。然后,应用程序会在列表中显示用户的联系人。单击列表中的某个联系人后,该联系人的数据(姓名、地址、电话等)将显示在模式页面或其他页面中。

我能够根据登录用户的 uid 显示联系人列表,问题是如何检索该单个联系人的数据并将其显示在屏幕上?

这是迄今为止我的代码:

这是保存从项目工厂检索到的联系人的 ionic 列表。

 <ion-list option-buttons="itemButtons" can-swipe="true"  class="item-remove-animate"  show-delete="data.showDelete" show-reorder="data.showReorder">
  <ion-item class="item-divider">Contacts(from fire):</ion-item>
  <ion-item ng-repeat="item in items track by $index" id="contactItem" ng-click="clickContact(); getAllContacts()">{{item.contactName}}

    <ion-delete-button class="ion-minus-circled" ng-click="onItemDelete(item)"> </ion-delete-button>

    <ion-option-button class="button-assertive" ng-click="edit(item)">Edit </ion-option-button>

    <ion-option-button class="button-calm" ng-click="share(item)">Share</ion-option-button>

    <ion-reorder-button class="ion-navicon" on-reorder="moveItem(item, $fromIndex, $toIndex)"></ion-reorder-button>

  </ion-item>

</ion-list>

这是项目工厂(我删除了 firebase 数据库名称并将其替换为 *****)

 .factory("items", function($firebaseArray) {// factory for getting contact items
   var userContacts=currentU.uid;// grab current user
   var itemsRef = new     Firebase("https://******.firebaseio.com/users/"+userContacts+"/contacts");
    return $firebaseArray(itemsRef);
  })

这是用户单击单个联系人时的 Controller 。目前,除了打​​开一个空模态框之外,它没有做太多事情。

$scope.clickContact = function(){// function when clicking on single contact for single contact view
      $scope.oModal2.show();// show/open contact modal when clicked on single contact };

最后,这是 Firebase 结构的示例。将来,每个联系人都会获得更多信息。

 users{
     63efb2b1-9b39-4cdb-9d57-b721e45f97c4{ //user id key
     contacts{
     -K8q4YOETVNh_LujTZD7 { //key for that specific contact
      contactName: "first contact"
      }
     -K8qE2LjUMK9IOd1OXQQ{ //key for that specific contact
     contactName: "second contact" 
     }
      }
     }
    }

我已经尝试对此进行研究,但还没有完全找到我正在寻找的答案,也许我没有提出正确的问题。

任何帮助和建议将不胜感激。 非常感谢!

最佳答案

要查找用户的特定联系人,您可以执行以下操作

var refUserContacts = new Firebase("https://******.firebaseio.com/users/"+userContacts+"/contacts");

refUserContacts.orderByChild("contactName").equalTo("first contact").on("value", function(data){
   console.log(data.val());
});

这将为您提供所需的联系方式。您应该考虑添加索引 contactName 以获得更好的查询性能。

关于javascript - 使用 ionic 和 firebase 检索点击时的特定数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35004616/

相关文章:

javascript - 根据数据属性、最小值和最大值隐藏项目

javascript - 您可以使用 python 中的映射或其他高阶函数来跟踪索引吗?

html - 为什么我的视频背景没有出现,即使代码看起来是正确的?

html - 垂直侧面菜单的折叠不起作用

firebase - E/FirebaseInstanceId( 5789) : Topic sync or token retrieval failed on hard failure exceptions: AUTHENTICATION_FAILED. 不会重试操作

javascript - 更快地将数组字段复制到另一个数组

javascript - 在网格中绘制单元格

json - 如何在Fluter上从Firestore中的模型json数据内部序列化/建模

firebase - 为云函数设置 Firebase init 目录

javascript - 如何制作我自己的屏蔽输入定义