2014-12-06 89 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); 
       }); 
      }); 
     } 
    } 
}]); 
相關問題