我目前正在尝试使用 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/