Denormalized Loop Data Loop

I have categories and subcategories.

A data structure similar to blog shows:

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

Now I got the data this way (for only one category): [controller]

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

And category service

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

It works well!

But now I need to get a list of subcategories in the category and associate it with the scope, and I don't know how ... I currently have this:

View:

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

Controller:

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

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

      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...:(
              });
        });

      },

Don't know how to associate this with angular view

+3
source share
1 answer

-, AngularFire ( 0,6), API , , , , .

-, , , . , .

, :

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

, :

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"));
}

, , , , $scope.category, $scope.subcategories:

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

Source: https://habr.com/ru/post/1539634/


All Articles