我有这个问题...我正在使用ng-repeat with inner来获取孩子内部的数据,这很有效,但我找不到获得ng-repeat长度的方法。我需要了解评论的次数。
如何使用inner?:
获得ng-repeat长度 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>
我正在使用Firebase:
注意:{{PostList.PostedComments.length}}无效...
我在控制器中尝试此错误时会出现此错误: angular.js:13920 TypeError:无法读取未定义的属性“length”。 这就是我所做的:
var value = PostList.PostedComments.length;
console.log('评论值为:'+ value);
但是当我尝试这个时它工作正常:
var value = PostList.length;
console.log('评论值为:'+值);
答案 0 :(得分:1)
如果你可以在PostList.PostedComments
上进行重复,那么你没有理由不能直接从中获取长度。
<div ng-repeat="innerPostList in PostList.PostedComments">
<p>{{innerPostList.comments}}</p>
</div>
<!-- length -->
<h4>{{PostList.PostedComments.length}}</h4>
编辑:我刚刚对firebase进行了一些研究,似乎你的$ firebaseArray()为实际的数据/结果返回一个类似于promise的包装器,所以这可能就是为什么它没有直接拥有你的属性想。当您检查网络请求/承诺时,它还可能尚未解决。
而不是var firebaseRefs = firebase.database().ref('PostUsers/');
$scope.PostList = $firebaseArray(firebaseRefs);
尝试这样的事情:
firebase.database().ref('PostUsers/').$loaded().then(function(data) {
$scope.PostList = data;
});
检查数据对象并确保它符合您的要求。
有用的参考资料: