我正在尝试返回从$http.get获取到的值,但无法使其起作用...
$scope.getDecision = function(id) {
var defer = $q.defer();
$http({
method: 'GET',
url: 'http://127.0.0.1:3000/decision',
params: {id: id},
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).success(function(data, status, header, config) {
console.log(data); //----> Correct values
defer.resolve(data);
}).error(function(data, status, header, config) {
defer.reject("Something went wrong when getting decision);
});
return defer.promise;
};
$scope.selectView = function(id) {
var decision = $scope.getDecision(id);
console.log(decision); //----> undefined
}
当我调用selectView时,我希望得到一个决定,但是我得到的只是未定义的...我是否误解了Promise模式?:S
return
一个简单的值。 - Bergiundefined
? - Bergi