use*_*050 2 javascript angularjs firebase angularfire
我有以下数据结构
{
"advisors" : {
"0ea9bab6-415e-4900-8698-ac03a1ef4518" : {
"description" : "",
"price" : 0,
"title" : ""
},
"cc31c353-ca6a-440d-b188-af2016f72aef" : {
"description" : "",
"price" : 0,
"title" : ""
},
"d8403e9b-fb74-4425-874f-2d125cd07a68" : {
"description" : "I know every corner in the arab countries.",
"price" : "50",
"title" : "Best guide in the middle east"
}
},
"advisors-countries" : {
"0ea9bab6-415e-4900-8698-ac03a1ef4518" : {
"Angola" : "true"
},
"cc31c353-ca6a-440d-b188-af2016f72aef" : {
"Angola" : "true"
}
},
Run Code Online (Sandbox Code Playgroud)
我想提出以下问题:在列表中给我安哥拉顾问的所有价格.
我尝试了下一个代码,仅检索Adola = true的Advisor的国家/地区.
var advisors = (ref.orderByChild("Angole").equalTo(true));
$scope.users = $firebaseArray(advisors);
console.log("sync ", $scope.users);
Run Code Online (Sandbox Code Playgroud)
但我一直在顾问对象中得到null对象.
要使用AngularFire轻松查询,您需要重新设计数据结构.
"advisors-countries" : {
"0ea9bab6-415e-4900-8698-ac03a1ef4518" : {
"Angola" : "true",
"price": 0
},
"cc31c353-ca6a-440d-b188-af2016f72aef" : {
"Angola" : "true",
"price": 0
}
}
Run Code Online (Sandbox Code Playgroud)
您应该在该advisors-countries位置包含价格和任何其他相关数据.如果您担心重复数据,那没关系.您可以使用多路径更新来保持数据的一致性.
然后你的查询将工作:
var query = ref.orderByChild("Angola").equalTo(true);
Run Code Online (Sandbox Code Playgroud)
现在,为什么你看到null $scope.users.
当您登录$scope.users到控制台时,它将是null因为最初没有数据.下载数据是一种异步操作,因此一开始就不可用.
要调试此操作,请使用$loaded().
$scope.users = $firebaseArray(advisors);
$scope.users.$loaded().then(function(data) {
console.log(data);
console.log(data === $scope.users);
});
Run Code Online (Sandbox Code Playgroud)
在第一个日志中,您将获得存在的任何数据.在第二个日志中,您将看到dataparam与$scope.users对象相同.这意味着您没有使用$loaded(),因为当它变得可用时,它将在您的模板中使用.
| 归档时间: |
|
| 查看次数: |
1141 次 |
| 最近记录: |