2014-12-06 65 views
1
getTopicContent.request().finally(function(){ 
    $scope.loader= false; 
}).then(function(response){ 
    $scope.threadContent = response.data; 


}) 


$scope.loadPages = function($scope) { 
    console.log($scope.threadContent.totalPages); 
} 

它返回undefined的threadContent。 getTopicContent是服務,但我希望$ scope.threadContent可以與$ scope.loadPages函數共享?

回答

1

由於您正在異步加載內容,因此不能期望數據以同步方式可用。在訪問數據之前,函數應該依賴promis解析。例如,你可以重寫你這樣的代碼:

function getContent() { 
    return getTopicContent.request().then(function (response) { 
     return response.data; 
    }).finally(function() { 
     $scope.loader = false; 
    }); 
} 

$scope.loadPages = function ($scope) { 
    getContent().then(function(data) { 
     $scope.threadContent = data; 
     console.log($scope.threadContent.totalPages); 
    }); 
} 

而且閱讀相關的問題description

+0

我得到了一個未定義的代碼不是函數錯誤。 – mike19911 2014-12-06 08:38:35

+0

對不起,錯字。它應該是'getContent()。然後'。在答案中更新了代碼。 – dfsq 2014-12-06 08:39:39

+0

你建議的流程和概念是有意義的,但我想知道它爲什麼說未定義的功能。 – mike19911 2014-12-06 08:40:03

相關問題