当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - sharing $scope between 2 function within one controller

问题描述:

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);

});

}

}

}]);

I got an error saying push of undefined. loadPages only fire when the user scrolled to the bottom of the page so I don't think it's an async issue, probably $scope problem? getTopicContent is my service.

网友答案:

You can use promise for async working and remove parameter $scope in loadPages like that:

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); 
                 });
            });
        }
    }
}]);
分享给朋友:
您可能感兴趣的文章:
随机阅读: