【问题标题】:Retrieving Specific data on click with ionic and firebase使用 ionic 和 firebase 检索点击时的特定数据
【发布时间】:2016-05-02 11:19:09
【问题描述】:

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

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

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

到目前为止,这是我的代码:

这是包含从项目工厂检索到的联系人的离子列表。

 <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);
  })

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

$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" 
     }
      }
     }
    }

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

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

【问题讨论】:

  • 您是否要获取用户的所有联系人?还是用户的一个特定联系人?

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


【解决方案1】:

要查找用户的特定联系人,您可以这样做

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

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

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

【讨论】:

    猜你喜欢
    • 2017-01-19
    • 2019-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-01
    • 2018-12-09
    • 1970-01-01
    相关资源
    最近更新 更多