【发布时间】:2015-05-18 09:49:01
【问题描述】:
我目前正在开发一个使用 firebase 和 angularJS(离子)的应用程序。基本上,这是一个汽车管理应用程序,因此您可以让人们与他人分享他们的汽车。我试图将数据结构尽可能平坦以提高效率。我的问题是,如果没有问题我可以显示与登录用户共享的不同汽车的 car_id 列表,我找不到显示与显示年份和型号的用户共享的汽车列表的方法。
提前感谢您的帮助!
{
"rules": {
"users": {
".write": true,
"$uid": {
".read": "auth != null && auth.uid == $uid"
},
"cars": {
"car_id":true,
"role":true // Owner, borower...
}
},
"cars": {
"car_id":true,
"model":true,
"year":true
}
}
}
carapp.controller("carsController", function($scope, $firebaseObject, $ionicPopup, $ionicHistory) {
$ionicHistory.clearHistory();
$scope.list = function() {
frbAuth = frb.getAuth();
if(frbAuth) {
var userObject = $firebaseObject(frb.child("users/" + frbAuth.uid));
userObject.$bindTo($scope, "user");
$scope.cars = frb.child("cars");
}}
$scope.createCar = function() {
$ionicPopup.prompt({
model: 'Create a new car',
inputType: 'text'
})
.then(function(result) {
if(result !== "") {
var newCar = $scope.cars.push({
model: result
})
var newCarId = newCar.key();
$scope.user.cars.push({car_id: newCarId, role: "owner" });
} else {
console.log("Action not completed");
}
});
}
});
<div class="list">
<a ng-repeat="car in user.cars" >
<h2>{{car.car_id}}</h2> ----> works fine !
<h2>{{car.model}}</h2> ----> How to get this working ? <h2>{{car.year}}</h2> ----> How to get this working ?
</a>
</div>
【问题讨论】:
-
在深入研究这个兔子洞之前,阅读 AngularFire guide 和 Firebase web guide 可以省去很多麻烦。不要在 Firebase 数据中使用数组(使用 users/$user/cars/$carid/true 到 create an index),也不要在控制器中使用 getAuth()——一个同步请求——来检测身份验证(在你的路由中使用 resolve 方法)。