Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
由于循环-AngularJS,post.length保持为0_Angularjs_Loops_Pagination - Fatal编程技术网

由于循环-AngularJS,post.length保持为0

由于循环-AngularJS,post.length保持为0,angularjs,loops,pagination,Angularjs,Loops,Pagination,我试图添加分页,但我似乎无法理解最后一部分。 一切都已设置好,尽管我的分页没有记录与用户链接的帖子数量 看到我正在执行forEach和if循环,并将检索到的项推送到一个空集合中,我的“posts.length”返回0 因此,分页仅显示第1/1页,而不是第1/2页(例如) 这是我的全部代码: profileCtrl.js 下面是$http.get-我正在尝试获取登录用户在执行此循环时发布的所有帖子: app.controller('profileCtrl', function($scope, au

我试图添加分页,但我似乎无法理解最后一部分。 一切都已设置好,尽管我的分页没有记录与用户链接的帖子数量

看到我正在执行forEach和if循环,并将检索到的项推送到一个空集合中,我的“posts.length”返回0

因此,分页仅显示第1/1页,而不是第1/2页(例如)

这是我的全部代码:

profileCtrl.js

下面是$http.get-我正在尝试获取登录用户在执行此循环时发布的所有帖子:

app.controller('profileCtrl', function($scope, auth, $http, $log) {

    $scope.auth = auth;
    $scope.date = auth.profile.created_at;
    $scope.pageSize = 5;
    $scope.posts= [];

    $http.get('URL')
        .then(function(result) {
            angular.forEach(result.data, function(data, key) {
                if(data.userId === auth.profile.user_id) {
                    $scope.posts.push(data);
            } 
        });
    });
});
profile.html 如您所见,我正试图使用
total items=“posts.length”
获取post中的post长度:

当I
console.log(posts.length)时我一直得到0,我猜这是因为
$scope.posts=[]在顶部声明(profileCtrl.js)

编辑:

在对console.log进行了一些调试之后,我确实得到了执行此操作时给出的值:

$http.get('url')
        .then(function(result) {
            angular.forEach(result.data, function(data, key) {
                if(data.userId === auth.profile.user_id) {
                    $scope.posts.push(data);
            } 
        });
        console.log($scope.posts.length);
    });
如何解决此问题?

如果在加载集合(使用分页)之前等待返回数据,请向容器中添加
ng If=“posts.length”
,或将
$scope.posts
初始化为
null
,然后添加
ng If=“posts”
如果希望在API返回0结果时显示列表。这将阻止对引导程序的分页指令进行解析,直到它需要的数据可用为止

编辑:调试后,以下plunkr包含一个工作实现:

如果您在加载集合之前等待返回数据(使用分页),请将
ng If=“posts.length”
添加到容器中,或将
$scope.posts
初始化为
null
,然后添加
ng If>=如果希望列表在API返回0结果时显示,请执行“posts”
。这将阻止对引导程序的分页指令进行分析,直到它需要的数据可用为止


Edit:调试后,以下plunkr包含一个有效的实现:

您的评论没有什么意义,阻止显示$scopes?从您所说的来看,pagination指令似乎没有检测到绑定到
total items
的值的变化,因此我的建议是延迟pa的呈现gination组件,直到posts数组被填充,这是您希望看到的。抱歉,我的意思是,这只是防止在没有找到POST时显示分页。但是延迟并不能解决问题。(删除我上面的评论,因为它确实没有任何意义,抱歉)你能把一个plunkr放在一起演示吗?因为如果延迟解决不了,一切都会好起来的。好吧,我也在尝试一个plunkr,我也遇到了同样的问题,我会继续更新plnkr中发布的所有内容,它会起作用。谢谢你的帮助。你的评论没有什么意义,阻止$scopes显示?从什么角度看您已经说过,pagination指令似乎没有检测到绑定到
total items
的值的变化,因此我的建议是延迟分页组件的呈现,直到填充了posts数组,这是您希望看到的结果。抱歉,我的意思是,这只是阻止对显示是否找不到帖子。但延迟并不能解决问题。(删除我上面的评论,因为它确实没有任何意义,对不起)你能把一个plunkr放在一起演示吗?因为如果延迟解决不了,一切都会好起来的。好吧,我也在尝试一个plunkr,我也有同样的问题,我会继续更新plnkr中发布的所有内容,它会起作用。谢谢你的帮助。
app.filter('startFrom', function() {
  return function(data, start) {
    return data.slice(start);
  }
});
$http.get('url')
        .then(function(result) {
            angular.forEach(result.data, function(data, key) {
                if(data.userId === auth.profile.user_id) {
                    $scope.posts.push(data);
            } 
        });
        console.log($scope.posts.length);
    });