显示已排序 Firebase 数组的特例(AngularJS)
Displaying Special Case Of Sorted Firebase Arrays (with AngularJS)
所以基本上我有一组俱乐部和一组用户。每个俱乐部和每个用户都有特定的经纬度。使用 ng-repeat 向用户显示所有俱乐部很容易,但我试图只显示每个用户一定距离内的俱乐部。那么到目前为止我想出了什么:
var arr = [];
$scope.clubs.$loaded().then(function() {
angular.forEach($scope.clubs, function(value, key) {
if($scope.getDistance(value.lng, value.lat) == true) {
console.log(value.name + " it's close enough!");
arr.push( {id: value.$id, description: value.description, name: value.name});
}
});
console.log(arr);
});
$scope.narr = arr;
有没有更优雅的方法来做到这一点?我基本上是从 firebase 数组中挑选出数据,然后将其放入一个较小的数组 (arr) 中,然后 ng-repeating 遍历它。但现在它不是火力阵列......有什么想法吗?
p.s。我不认为我可以使用任何内置的 firebase 排序,因为距离对于每个用户都是唯一的,并且每个位置都需要纬度和经度。
为什么不在您的模板中评估 $scope.getDistance()
?
您只需在 $scope
上创建一个 $firebaseArray()
。
$scope.cabyns = $firebaseArray(ref);
然后在您的模板中,您可以使用 ng-repeat
中的项目调用 $scope.getDistance()
。使用 ng-if
方向,您可以声明是否应该加载它。
<ul>
<li ng-repeat="item in cabyns">
<span ng-if="getDistance(item)">
{{ item.name }}
<span>
</li>
</ul>
如果你正在做地理围栏,你可以尝试做的另一件事是使用 GeoFire。
所以我选择了 GeoFire。它与上面的实现几乎相同,但在缩放方面应该更好。如果有人感兴趣,这就是您的 GeoFire 函数的样子:
var array = [];
$scope.publicArray = array;
$scope.geoFunc = function() {
geoQuery = geoFire.query({
center: [userData.Latitude, userData.Longitude],
radius: 25
});
geoQuery.on("key_entered", function(key, location, distance) {
console.log(key + " is located at [" + location + "] which is within the query (" + distance.toFixed(2) + " km from center)");
var newRef = new Firebase(furl + "/clubs/" + key);
$scope.thing = $firebaseObject(newRef);
array.push($scope.thing);
});
};
然后你可以通过你的 publicArray ng-repeat。
所以基本上我有一组俱乐部和一组用户。每个俱乐部和每个用户都有特定的经纬度。使用 ng-repeat 向用户显示所有俱乐部很容易,但我试图只显示每个用户一定距离内的俱乐部。那么到目前为止我想出了什么:
var arr = [];
$scope.clubs.$loaded().then(function() {
angular.forEach($scope.clubs, function(value, key) {
if($scope.getDistance(value.lng, value.lat) == true) {
console.log(value.name + " it's close enough!");
arr.push( {id: value.$id, description: value.description, name: value.name});
}
});
console.log(arr);
});
$scope.narr = arr;
有没有更优雅的方法来做到这一点?我基本上是从 firebase 数组中挑选出数据,然后将其放入一个较小的数组 (arr) 中,然后 ng-repeating 遍历它。但现在它不是火力阵列......有什么想法吗?
p.s。我不认为我可以使用任何内置的 firebase 排序,因为距离对于每个用户都是唯一的,并且每个位置都需要纬度和经度。
为什么不在您的模板中评估 $scope.getDistance()
?
您只需在 $scope
上创建一个 $firebaseArray()
。
$scope.cabyns = $firebaseArray(ref);
然后在您的模板中,您可以使用 ng-repeat
中的项目调用 $scope.getDistance()
。使用 ng-if
方向,您可以声明是否应该加载它。
<ul>
<li ng-repeat="item in cabyns">
<span ng-if="getDistance(item)">
{{ item.name }}
<span>
</li>
</ul>
如果你正在做地理围栏,你可以尝试做的另一件事是使用 GeoFire。
所以我选择了 GeoFire。它与上面的实现几乎相同,但在缩放方面应该更好。如果有人感兴趣,这就是您的 GeoFire 函数的样子:
var array = [];
$scope.publicArray = array;
$scope.geoFunc = function() {
geoQuery = geoFire.query({
center: [userData.Latitude, userData.Longitude],
radius: 25
});
geoQuery.on("key_entered", function(key, location, distance) {
console.log(key + " is located at [" + location + "] which is within the query (" + distance.toFixed(2) + " km from center)");
var newRef = new Firebase(furl + "/clubs/" + key);
$scope.thing = $firebaseObject(newRef);
array.push($scope.thing);
});
};
然后你可以通过你的 publicArray ng-repeat。