I have the two following tables with services and categories:
- servicecat
- catid
- name : "Category Name"
- services
- srvid1 : true
- srvid2 : true
- srvid3 : true
- services
- srvid
- name: "Service Name"
- Details: "blabla"
- servicecat:
- catid1 : true
- catid2 : true
I'm trying to build a firebaseArray to show when a user click on a service its information and categories.
<script>
var app = angular.module("sampleApp", ['firebase']);
var fb = firebase.database().ref();
app.factory("getServices", ["$firebaseArray",
function($firebaseArray) {
var ref = firebase.database().ref().child("services");
return $firebaseArray(ref);
}
]);
app.controller("mainController", ["$scope", "$firebaseArray","$firebaseObject","getServices",
function($scope, $firebaseArray, $firebaseObject, getServices) {
$scope.services = getServices;
$scope.postFullService = function(srvid) {
var ref = fb.child('services/'+srvid);
$scope.selectSrvtoPost = $firebaseObject(ref);
var ref2 = fb.child('services/'+srvid+'/servicecat/');
var list = $firebaseArray(ref2).$loaded()
.then(function(snap) {
$scope.selectCategories = snap;
snap.forEach(function(snap2) {
$scope.selectCategories.catid = snap2;
var id = $scope.selectCategories.catid.$id;
var ref3 = fb.child('servicecat/'+id);
var list2 = $firebaseObject(ref3).$loaded()
.then(function(snap3) {
$scope.selectCategories.catname = snap3;
})
})
})
.catch(function(error) {
console.log("Error: ", error);
})
}
}
]);
</script>
<body ng-app="sampleApp">
<div ng-controller="mainController">
<h1>Services List</h1>
<ul>
<li ng-repeat="service in services" ng-click="postFullService(service.$id)" class="click"><strong>{{ service.title }}</strong><br>{{ service.details }}</li>
</ul>
<h2>Service Selected</h2>
<ul>
<li><strong>{{ selectSrvtoPost.title }}</strong><br>{{ selectSrvtoPost.details }}</li>
<h3>Selected Categories</h3>
<hr>
{{ selectCategories }}
<hr>
{{ selectCategories.catid }} <br> {{ selectCategories.catid.$id }} <br> {{ selectCategories.catname.name }} <hr>
<br><strong>UL:</strong>
<ul>
<li ng-repeat="catselect in selectCategories">
{{ catselect.$id }} <br>
Catid : {{ catselect.catid }}
</li>
</ul>
</div>
</body>
I can't seem to find how to make it work. I achieve to show the result in the console but I can't create a firebaseArray that I could use in DOM to manipulate the two tables and its records linked. It seems that I should be using chaining promises to solve the problems but I don't really understand how it works.