0

I have this issue... I'm using ng-repeat with inner to obtain the data inside a child, this works perfectly but I cannot find the method to get the length of the ng-repeat. I need to get the count of the comments.

How to obtain the ng-repeat length using inner?:

   js:
    angular.module('myApp.Profile', ['ngRoute', 'firebase'])
   .config(['$routeProvider', function($routeProvider) {
   $routeProvider.when('/Profile', {
   templateUrl: 'Profile/Profile.html',
   controller: 'ProfileCtrl'
   });
   }])

.controller('ProfileCtrl', ['$scope', '$firebase', '$firebaseArray', '$firebaseObject', function ($scope, $firebase, $firebaseArray, $firebaseObject) {

   var firebaseRefs = firebase.database().ref('PostUsers/');
   $scope.PostList = $firebaseArray(firebaseRefs);

   }]);


   html:
  <body ng-controller="ProfileCtrl">
  <div ng-repeat="PostList in PostList"> 

  <p>Author: {{PostList.userAuthor}}</p> 
  <p>Comments: {{PostList.post}}</p> 

  <div ng-repeat="innerPostList in PostList.PostedComments">

   <p>Author: {{innerPostList.username}}</p>
    <p>Comments:{{innerPostList.comments}}</p> 

 </div> 
</div> 

 <h4>Publications number: {{PostList.length}}</h4>
<h4>Comments number: {{????.length}}</h4> 

</body>

I'm using Firebase:

enter image description here

NOTE:{{ PostList.PostedComments.length}} is not working...

this error appears when I try this in the controller: angular.js:13920 TypeError: Cannot read property 'length' of undefined. This is what I did:

var value = PostList.PostedComments.length;

console.log('the comments value is:' + value);

but when I try this it works fine:

var value = PostList.length;

console.log('the comments value is:' + value);

1 Answers1

1

If you can ng-repeat on PostList.PostedComments, theres no reason you can't just grab the length from it directly.

<div ng-repeat="innerPostList in PostList.PostedComments">
   <p>{{innerPostList.comments}}</p>
</div>

<!-- length -->
<h4>{{PostList.PostedComments.length}}</h4>

EDIT: I just did some research on firebase and it seems that your $firebaseArray() returns a promise-like wrapper for the actual data/results, so thats probably why it doesn't directly have the properties on it that you want. Its also possible that the network request/ promise hasn't resolved yet when you are inspecting it.

Instead of var firebaseRefs = firebase.database().ref('PostUsers/'); $scope.PostList = $firebaseArray(firebaseRefs);

Try something like this:

firebase.database().ref('PostUsers/').$loaded().then(function(data) {
   $scope.PostList = data;
});

Inspect the data object and make sure it has what you want.

Useful references:

Community
  • 1
  • 1
matmo
  • 1,339
  • 12
  • 17
  • technically it should work. What happens when you capture the length in the controller and do a console.log for that. Do you see an error? – Gary Aug 18 '16 at 03:14
  • this error appears when I try this in the controller: angular.js:13920 TypeError: Cannot read property 'length' of undefined, this is what I did: var value = PostList.PostedComments.length; console.log('the comments value is:' + value); but when I try this it works fine: var value = PostList.length; console.log('the comments value is:' + value); – Software Costa Rica Aug 18 '16 at 03:37
  • PostList is contaning the firebaseArray as expected but PostList.PostedComments is undefined... – Software Costa Rica Aug 18 '16 at 04:28
  • I removed the PostedComments from PostList,PostedComments and it is actually also containing the firebaseArray : PostList.length – Software Costa Rica Aug 18 '16 at 04:32