2014-02-07 3 views
3

범주와 하위 범주가 있습니다.AngularFire 루프 비표준 데이터

데이터 구조는 blog 방송 같다 : [제어부]

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

및 카테고리 서비스

을 :

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

가 지금은 (단 하나의 카테고리에 대한) 데이터를 이런 식으로 검색하려면

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

뷰 :

<div ng-init="findSubCategories()"> 
<li class="list-group-item" ng-repeat="subCategory 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", 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...:(
       }); 
     }); 

     }, 

내가, API가 상당히 변경되었습니다 최신 AngularFire 버전 (현재 0.6)로 업그레이드하는 것이 좋습니다 것,

답변

2

첫 번째 각도 전망이 바인딩하는 방법을 잘 모릅니다 그것은 쉬울 수 있습니다 네가하려는 일을 성취해라.

둘째, 각 카테고리에 대해 하나의 바인딩 만 만들어야하며 하위 카테고리 ID는 해당 하위 노드이므로 해당 데이터에 자동으로 포함됩니다. 그런 다음 하위 카테고리 이름에 대해 별도의 바인딩을 만들어야합니다. 그런 다음 ID를 조회 할 수 있습니다.

는 이제 데이터가 다음과 같다고 가정 해 보겠습니다

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에서 추출한 ID를 사용하여 $scope.subcategories의 하위 카테고리 이름을 얻으십시오.

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

카테고리 및 하위 카테고리가 아니지만 게시물 및 사용자가 있으며 한 명의 사용자가 게시물 목록을 표시하려면이 방법이 올바른 해결책입니까? 많은 게시물과 많은 사용자가있을 수 있습니다. 이 작업을 수행하는 데 여러 가지 방법을 시도했지만 올바른 작업을 찾지 못하는 것 같습니다. –

+0

하위 범주 목록이 실제로 길 경우 모든 항목을로드하는 것이 적절하지 않을 수 있습니다. 이 경우, 각 하위 카테고리에 대해 새로운 $ firebase 인스턴스를 생성하여 하나씩 가져올 수 있습니다 (아니면 일반 Firebase API를 사용하는 것일 수도 있습니다). – Anant