2014-12-06 67 views
0
app.controller('AppCtrl', ['$scope', '$http','$stateParams','getTopicContent', function($scope,$http,$stateParams,getTopicContent){ 

    $scope.ionLoader = true; 

    getTopicContent.request($stateParams.topicId).then(function(response){ 
     $scope.threadContent = response.data; 

    }).finally(function(){ 
     $scope.ionLoader = false; 
    }); 


    $scope.loadPages = function ($scope) { 
     if(totalPages > 1){ 
       $http({ 
       url: "http://someurl.php", 
       method: "GET", 
       params: {topicId: $stateParams.topicId,page : 2} 
      }).success(function(data){ 
       $scope.threadContent.push(data); 
      }); 
     } 
    } 


}]); 

我得到一个错误,说推送未定义。当用户滚动到页面底部时,loadPages只会触发,所以我不认为这是一个异步问题,可能是$ scope问题? getTopicContent是我的服务。

+0

你为什么加入loadPages功能$范围参数?你可以重写$ scope。 – xio4 2014-12-06 09:53:20

+0

@ xio4是不是问题的原因? – mike19911 2014-12-06 09:59:52

+0

你的'getTopicContent.request()'返回一个'promise'吧?我强硬这是一个异步问题,或者试着首先在你的$ scope中定义'$ scope.threadContent = []'或'null'。并确保你的'$ scope.threadContent'存在之前推动它。 – 2014-12-06 10:30:32

回答

0

您可以使用异步工作的承诺,并删除loadPages这样的参数$范围:

app.controller('AppCtrl', ['$scope', '$http','$stateParams','getTopicContent', function($scope,$http,$stateParams,getTopicContent){ 

    $scope.ionLoader = true; 

    var promise = getTopicContent.request($stateParams.topicId).then(function(response){ 
     $scope.threadContent = response.data; 

    }).finally(function(){ 
     $scope.ionLoader = false; 
    }); 


    $scope.loadPages = function() { 
     if(totalPages > 1){ 
       $http({ 
       url: "http://someurl.php", 
       method: "GET", 
       params: {topicId: $stateParams.topicId,page : 2} 
      }).success(function(data){ 
       promise.then(function(none) { 
        $scope.threadContent.push(data); 
       }); 
      }); 
     } 
    } 
}]);