AngularFire循环非规格化数据 [英] AngularFire Loop denormalized data

查看:125
本文介绍了AngularFire循环非规格化数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有类别和子类别。



数据结构就像 blog 显示:

 类别:{
-JF1RmYehtF3IoGN9xHG(categoryId):{
title:Example,
子类别:{
JF1RmYehtF3IoGN239GJ(subCategoryId):true
}

到现在为止我以这种方式检索数据(只有一个类别):[controller]

  $ scope.findOneCategory = function(categoryId){
angularFire(Categories.find(categoryId),$ scope,'category');

$ / code>

和分类服务

  find:function(categoryId){
return FireRef.categories()。child('/'+ categoryId);
},

这个效果很好!

但现在我需要检索类别的子类别列表,并将其绑定到范围,我不知道如何...我目前这个:

查看:

 < div ng-init =findSubCategories()> 
< li class =list-group-itemng-repeat =subCategories in subCategories> {{subCategory.name}}< / li>

控制器:

  $ scope.findSubCategories = function(){
angularFire(Categories.findSubCategories($ routeParams.categoryId),$ scope,'subCategories');

服务看起来像这样我尝试了一些东西,不知道它应该是什么样子...如果有人可以帮助,我会很高兴!

  findSubCategories :function(categoryId){

var subCategoriesIdsList = FireRef.categories()。child('/'+ categoryId).child('subcategories');

subCategoriesIdsList.on(child_added,function(snap){
FireRef.subcategories()。child(/+ snap.name())。once(value函数(snap){
//渲染链接页面上的注释
console.log(snap.val());(我在控制台中看到对象但不知道如何绑定它以... :(
));
});

},

不知道如何将其与角度视图绑定

解决方案

首先,建议升级到最新的AngularFire版本(目前为0.6),API已经改变了很多,可能会更容易完成你想要做的事情。



第二,你应该只为每个类别创建一个绑定,并且子类别ID将自动包含在该数据中,因为它们只是子节点,那么你需要为子类别名称创建一个单独的绑定,w那么你可以查看你的ID。

假设你的数据如下所示:

  categories:{
-JF1RmYehtF3IoGN9xHG:{
title:Example,
subcategories:{
JF1RmYehtF3IoGN239GJ:true
}
}
},
subCategories:{
JF1RmYehtF3IoGN239GJ:{
name:Example Subcategory,$ b $您将创建两个绑定,一个用于类别,另一个用于子类别:
$ b

  function MyController($ scope,$ firebase){
var ref = new火力( HTTPS://<我的-火力> .firebaseio.com /);
$ scope.category = $ firebase(ref.child(categories /+ categoryID));
$ scope.subcategories = $ firebase(ref.child(subCategories));





最后,在你的视图中,使用你从 $ scope.category $ scope.subcategories 获取子类别名称:

 < ul ng-repeat =(id,val)in category.subcategories> 
< li> {{subcategories [id] .name}}< / li>
< / ul>


I have categories and subcategories.

The structure of data is like the blog shows:

categories: {
    -JF1RmYehtF3IoGN9xHG(categoryId): {
      title: "Example",
      subcategories: {
        JF1RmYehtF3IoGN239GJ(subCategoryId): true
      }

To now i retrieved the data this way(for just one category):[controller]

$scope.findOneCategory = function (categoryId) {
    angularFire(Categories.find(categoryId), $scope, 'category');
}

And Categories service

      find: function(categoryId) {
        return FireRef.categories().child('/'+categoryId);
      },

This works good!

But now i need to retrieve a list of subcategories in category and bind it to the scope and i dont know how...i have currently this:

The view:

<div ng-init="findSubCategories()">
<li class="list-group-item" ng-repeat="subCategory in subCategories">{{ subCategory.name }}</li>

The controller:

    $scope.findSubCategories = function() {
      angularFire(Categories.findSubCategories($routeParams.categoryId), $scope, 'subCategories');
    }

And the service look like this i tried something but it's wrong and have no idea how it should looks like... if anyone could help i would be so glad!

      findSubCategories: function(categoryId) {

        var subCategoriesIdsList = FireRef.categories().child('/'+categoryId).child('subcategories');

        subCategoriesIdsList.on("child_added", function(snap) {
              FireRef.subcategories().child("/"+snap.name()).once("value", function(snap) {
                // Render the comment on the link page.
                console.log(snap.val());(i see the object in console but have no idea how to bind it now with the view...:(
              });
        });

      },

dont know how to bind this with angular view

解决方案

First, I'd recommend upgrading to the latest AngularFire version (currently 0.6), the API has changed quite a bit and it might be easier to accomplish what you're trying to do.

Second, you should only create one binding for each category and the subCategory IDs will automatically be included in that data since they're just the child nodes. Then you'll need to create a seperate binding just for the subcategory names, which you can then look up your ID.

Let's say your data looks like this:

categories: {
  -JF1RmYehtF3IoGN9xHG: {
    title: "Example",
    subcategories: {
      JF1RmYehtF3IoGN239GJ: true
    }
  }
},
subCategories: {
  JF1RmYehtF3IoGN239GJ: {
    name: "Example Subcategory",
  }
}

You'll create two bindings, one for categories and another for subcategories:

function MyController($scope, $firebase) {
  var ref = new Firebase("https://<my-firebase>.firebaseio.com/");
  $scope.category = $firebase(ref.child("categories/" + categoryID));
  $scope.subcategories = $firebase(ref.child("subCategories"));
}

And finally, in your view, use the ID you extract from $scope.category to get the sub category name from $scope.subcategories:

<ul ng-repeat="(id, val) in category.subcategories">
  <li>{{subcategories[id].name}}</li>
</ul>

这篇关于AngularFire循环非规格化数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆