【问题标题】:ng-repeat based based on nested items基于嵌套项的 ng-repeat
【发布时间】:2015-11-18 04:04:55
【问题描述】:

根据以下数据,并假设有更多汽车和更多与特定汽车关联的 Firebase 用户 ID。我将如何只重复与当前用户匹配的汽车数据。示例:如果我已登录并且我的 Firebase.uid 是 simplelogin:26,我将如何重复包含我的 Firebase.uid 的汽车数据?所以在这种情况下,它只会显示本田。

我已经读到您组织 Firebase 数据的方式非常重要,所以我不确定这是否是理想的格式,无论哪种方式我都想知道这是否可能是它当前的格式。我仍然在学习这一切,似乎无法弄清楚这一点。任何见解都会很棒,谢谢!

html

<h1>MyCars</h1>
  <ul>
    <li ng-repeat="car in cars">     
      <p>Make: {{ car.type }}</p>
      <p>Year: {{ car.year }}</p>
      <p>color: {{ car.color }}</p>
    </li>
  </ul>

JSON

"cars": {
    "-JRHTHaIs-jNPLXOQivY": {
      "type": "Honda",
      "year": "2008",
      "color":"red",
      "simplelogin:26": {
       "name":"ted"
     },
      "simplelogin:32": {
       "name":"ted"
      }
    },
    "-JRHTHaKuITFIhnj02kE": {
      "type": "Chevy",
      "year": "2006",
      "color":"white",
      "simplelogin:16": {
       "name":"ted"
     }
   }
 }

【问题讨论】:

  • 您好!欢迎来到堆栈溢出。您没有在数据中包含 cars 的定义,或者您如何将其插入到 $scope (或任何有用的代码,真的)。请附上mcve,展示您迄今为止的尝试。

标签: angularjs firebase ng-repeat


【解决方案1】:

这不是一个理想的数据结构。如果汽车确实与用户有 1:1 的关系,如数据所示,那么应该简单地按用户存储它们,然后查询特定的用户 ID:

{
  "cars": {
      "ted": {
         ...
      }
  }
}

现在按用户查询汽车非常简单:

var ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com/");
$scope.cars = $firebaseArray(ref.child('cars/<USER ID>'));

如果汽车不能按用户拆分,因为它们具有 n:1 关系,那么 a query 可以提供相同的功能(确保您 index them on the server):

var ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com/");
var query = ref.child('cars').orderByChild('name').equalTo('ted');
$scope.cars = $firebaseArray(query);

如果您想建立 n:n 关系,则将用户索引到汽车更合适:

"cars": {
    "-JRHTHaIs-jNPLXOQivY": {
      "type": "Honda",
      "year": "2008",
      "color":"red"
     },
     ...
},

"owners": {
   "ted": {
      "-JRHTHaIs-jNPLXOQivY": true,
      ...
   }
}

现在为给定用户获取汽车有点困难,但仍然不无道理:

angular.factory('CachedCarList', function() {
  // a simple cache of Firebase objects looked up by key
  // in this case, a list of cars that have an n:n relationship to users
  var carsRef = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com/cars");
  var carsLoaded = {};

  return {
    get: function(carId) {
      if( !carsLoaded.hasOwnProperty(carId) ) {
        carsLoaded[cardId] = $firebaseObject(carsRef.child(carId));
      }
      return carsLoaded[carId];
    },
    destroy: function(carId) {
      angular.forEach(carsLoaded, function(car) {
        car.$destroy();
      });
      carsLoaded = {};
    }
  }
});

angular.factory('CarList', function($firebaseArray, CachedCarList) {
   var ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com/");

   var CarsIndexed = $firebaseArray.$extend({
     '$$added': function(snapshot) {
        // when something is added to the index, synchronize the actual car data
        // we use the $loaded promise because returning it here will make AngularFire
        // wait for that data to load before triggering added events and Angular's compiler
        return CachedCarList.get(snapshot.key()).$loaded();
     },

     '$$updated': function(snapshot) {
        return false; // our cars update themselves, nothing to do here
     }
   });

   return function(userId) {
      // when a list of cars is requested for a specific user, we return an CarsIndexed
      // than synchronizes on the index, and then loads specific cars by referencing their
      // data individually
      return new CarsIndexed(ref.child('owners/'+userId));
   }
});

firebase-util's NormalizedCollection 可以帮助简化此过程:

angular.factory('CarList', function($firebaseArray) {
  var ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com/");
  return function(userId) {
    var nc new Firebase.util.NormalizedCollection(
      ref.child('owners/' + userId),
      ref.child('cars')
    ).select('cars.type', 'cars.year', 'cars.color')
    return $firebaseArray(nc.ref());
  }
});

Firebase Angular guide 涵盖了许多类似的主题,并且还引入了一个绑定库来代表您处理同步远程/本地数据。

此外,Firebase 文档还涵盖了许多主题,例如数据结构、索引多对一或多对多关系等。我强烈建议您从前到后阅读the guide,然后再继续阅读。

【讨论】:

  • 感谢您的彻底回复和建议。看起来做对了,在我继续之前,我将不得不停下来(重新思考/了解更多)。这似乎增加了我尚未处理的另一个层次的复杂性,但我想现在是深入研究的好时机。大声笑,谢谢。
  • 在 NoSQL 中加入数据很困难。随着我们的迭代,Firebase 将继续让这一切变得更简单。目前,学习这些技术需要做一些工作,但是一旦你掌握了它们,它们就相当简单了。
【解决方案2】:

您可以使用语法ng-repeat="(key, value) in data" 迭代ng-repeat 中对象的属性

<ul>
    <li ng-repeat="(key, value) in cars">
        <p>Make: {{ value.type }}</p>
        <p>Year: {{ value.year }}</p>
        <p>color: {{ value.color }}</p>
    </li>
</ul>

【讨论】:

  • 感谢您的回复,仅此一项并没有解决我还必须添加的技巧:(ng-show="value.hasOwnProperty(Firebase.uid)),但它引导我正确方向。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-11-05
  • 2016-08-28
  • 1970-01-01
  • 1970-01-01
  • 2016-09-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多