2015-07-20 121 views
0

我需要在下面的函數中打印出localeData值。 我知道如何在功能方面打印,就像我在這裏所做的一樣。 我試過這個,但沒有工作。Angularjs獲取函數外的對象值

$http.post(SERVER_URL + 'getLocaleData').success(function(localeData) { 
     console.log(localeData); 
     }).error(function(err) { 
      alert('warning', err.message); 
     }); 
     //need to get that value here. 
     console.log(localeData); 

編輯

其實我需要做的就是這個

app.factory('customLoader', function($http, $q, SERVER_URL) { 

    return function(options) { 
     var deferred = $q.defer(); 

     // do something with $http, $q and key to load localization files 
     $http.post(SERVER_URL + 'getLocaleData').success(function(localeData) { 
     //do something here to localData 
     }).error(function(err) { 
      alert('warning', err.message); 
     }); 

     deferred.resolve(localeData); 
     return deferred.promise; 

    }; 
}); 

這正是我需要的。最後我需要發送localeData。

+0

你應該把這個輸出分配給控制器的對象/變量 – Vineet

+0

任何想法該怎麼做? – Mad

+0

你爲什麼要在功能外打印? –

回答

0

你不需要$ Q來實現自己的目標:

app.factory('customLoader', ["$http", "SERVER_URL", function($http, SERVER_URL) { 
    return function(options) { 
     return $http.post(SERVER_URL + 'getLocaleData'); 
    }; 
}]); 

因爲$http和所有它的方便的方法實際上是返回一個承諾。在這種情況下,你可以按如下方式使用該服務:

customLoader(options).success(function(localeData){ 
    //do something here to localData 
}).error(function(err){ 
    alert('warning', err.message); 
}); 

如果你真的必須使用$q,那麼這應該這樣做:

app.factory('customLoader', ["$http", "$q", "SERVER_URL", function($http, $q, SERVER_URL) { 
    return function(options) { 
     return $q(function(resolve, reject){ 
      $http.post(SERVER_URL + 'getLocaleData').success(function(localeData) { 
       //do something here to localData 
       resolve(localeData); // ultimately you ought to resolve inside this function 
      }).error(function(err) { 
       alert('warning', err.message); 
       reject(localeData); // ultimately you ought to reject inside this function 
      }); 
     }); 
    }; 
}]); 
+0

我得到了一個下面的錯誤。 TypeError:$ q不是功能 \t return $ q(function(resolve,reject){ – Mad

+0

好的,這是不太可能的。你如何使用這個服務?同時我對我提供的示例做了一個非常小的更新。 –

+0

不幸的是$ q是不是一個函數。 你肯定是它的功能? 我只是複製粘貼代碼。 – Mad

0

JS是異步的,這基本上意味着你的最後一個console.log將在服務器返回任何數據之前執行。所以,你必須這樣做既對您的承諾中:

$http.post(SERVER_URL + 'getLocaleData').success(function(localeData) { 
    console.log(localeData); 
    }).error(function(err) { 
     console.log(err); 
     alert('warning', err.message); //Just in case you didn't know, this line supposes that your server returns a JSON object with a "message"-property 

    }); 

這樣一來,你的服務器響應將是成功和失敗console.log「版兩種。

Reference

編輯:如果你真的想這樣做的另一種方式:

var response; //First, declare a variable in the parent scope. 

$http.post(SERVER_URL + 'getLocaleData').success(function(localeData) { 
    response = localeData; //Assign the response data to the variable. 
    console.log(localeData); 
    }).error(function(err) { 
     alert('warning', err.message); //Just in case you didn't know, this line supposes that your server returns a JSON object with a "message"-property 

    }); 
    console.log(response); 

如您所願的例子不會評價,雖然。最後一個console.log將只記錄undefined

+0

您也可以使用'q'服務。 – Vineet

+0

我知道如何做到這一點的功能。 – Mad

+0

你必須瞭解JS的體系結構。即使你試圖在功能之外訪問,這也是反模式的標誌。儘管如此,我會更新我的答案,試着讓你知道如何去做。 – Dencker

0

localeData僅限於.success函數的範圍。除非你指定其他變量,否則你不能在外面得到它。

var data = "" 
$http.post(SERVER_URL + 'getLocaleData').success(function(localeData) { 
    data = localeData; 
    }).error(function(err) { 
     alert('warning', err.message); 
    }); 
//need to get that value here. 
console.log(data); 

但是不要指望在data之前得到想要的值,直到得到迴應爲止。 $http.post異步

看看承諾克服這樣的問題,因爲它們使用的所有的地方,在世界的角度。

UPDATE:

當您在編輯所做的那樣,可以選擇使用$ Q服務,如:

app.factory('customLoader', function($http, $q, SERVER_URL) { 

    return function(options) { 
     var deferred = $q.defer(); 

     // do something with $http, $q and key to load localization files 
     $http.post(SERVER_URL + 'getLocaleData').success(function(localeData) { 
     //do something here to localData 
     }).error(function(err) { 
      alert('warning', err.message); 
     }); 

     deferred.resolve(localeData); 
     return deferred.promise; 

    }; 
}); 

或者,你可以簡單地返回$http.post這本身會返回一個承諾

app.factory('customLoader', function($http, $q, SERVER_URL) { 
    return function(options) {   
     return $http.post(SERVER_URL + 'getLocaleData'); 
    }; 
}); 

無論哪種方式,您將返回一個承諾對象,而不僅僅是從服務器獲得的實際響應。該localeData可以如下訪問:

customLoader().success(function(res){ 
    console.log(res); 
}).error(function(){ 
    //show some error message 
}) 
0

通過以下方式,您可以訪問您的服務器響應控制器外部工廠:

app.factory('customLoader', function ($http, $q) { 
     return { 
      response: {}, 
      getLocalData: function (param) { 
       var deferred = $q.defer(); 
       $http.post(SERVER_URL + 'getLocaleData', param). 
        success(function (data, status, headers, config) { 
         deferred.resolve(data); 
        }). 
        error(function (data, status, headers, config) { 

        }); 

       return deferred.promise; 
      } 
     } 
    }); 


app.controller('yourCtrl', function($scope,customLoader) { 

     $scope.GetLocaleData= function() {  
       customLoader.getLocalData({ 
        Role: role, 
        Email_Id: $scope.Email, 
        Pwd: $scope.Password 
       }).then(function(localeData) { 
        $scope.Data = localeData;       
       });   
     } 
}); 
1

您無需創建&解決你自己。有一個更簡單的方法。 (同樣在你給的例子中,你解決承諾之前,基礎阿賈克斯完成)

Angular.js $http服務接口$q接口呢!所以你可以寫:

app.factory('customLoader', function($http, $q, SERVER_URL) { 
    return function(options) { 
    var promise = $http.post(SERVER_URL + 'getLocaleData') 
     .then(function(localeDataBag) { 
     var localeData = localeDataBag.data; // .data of 'then' equals to what .success returns.    
     modifiedLocaleData = localeData++; // do something here to localData 
     return modifiedLocaleData; // This is the new result 
     }) 
     .catch(function(err) { 
     console.log(err); 
     return $q.reject(err); // This is the new result if something failed. Since we are chaining promises, we should keep failure state. 
     }); 
    return promise; 
    }; 
});