我需要多個區域的用戶詳細信息。我試圖用通用函數返回細節。但我得到的結果爲undefined
。我明白,我需要使用$differed
才能獲得重新使用。但我對目前的情況並不瞭解。
有人幫我在這裏嗎?
這裏是我的功能:
$scope.currentUserInfo = function() {
var userDetails;
server.getProfile.get().$promise.then(function (response) {
if(response.Message.toUpperCase().indexOf('SUCCESS') != -1) {
return userDetails = response;
}
return "Not able to get the user details this time"
})
return userDetails;
}
$scope.currentUser = $scope.currentUserInfo();
console.log($scope.currentUser) //undefined.
var function1 = function() {
$scope.currentUserInfo(); //once user details available do further
$scope.age = $scope.currentUser.age;
}
var function2 = function() {
$scope.currentUserInfo(); //once user details available do further
$scope.name = $scope.currentUser.name;
}
可能的重複[如何從異步調用返回響應?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-an-asynchronous-call) – Grundy