如何访问 Angularjs 中 $http.get 之外的结果
How to access the result outside the $http.get in Angularjs
如何在成功回调函数之外访问 $http.get
的结果?
我的密码是
$http.get('http://myserver/' + $scope.myE)
.success(function(data)
{
$scope.mydata = data;
});
alert(JSON.stringify($scope.mydata));
我没有定义。
您需要 return 承诺并在回调中发出警报:
// define the function that does the ajax call
getmydata = function() {
return $http.get('http://myserver/' + $scope.myE)
.success(function(data)
{
$scope.mydata = data;
});
}
// do the ajax call
getmydata().then(function(data) {
// stuff is now in our scope, I can alert it
alert($scope.mydata);
});
var promise = $http.get('http://server/');
promise.then(function(payload) {
alert(payload);
});
如何在成功回调函数之外访问 $http.get
的结果?
我的密码是
$http.get('http://myserver/' + $scope.myE)
.success(function(data)
{
$scope.mydata = data;
});
alert(JSON.stringify($scope.mydata));
我没有定义。
您需要 return 承诺并在回调中发出警报:
// define the function that does the ajax call
getmydata = function() {
return $http.get('http://myserver/' + $scope.myE)
.success(function(data)
{
$scope.mydata = data;
});
}
// do the ajax call
getmydata().then(function(data) {
// stuff is now in our scope, I can alert it
alert($scope.mydata);
});
var promise = $http.get('http://server/');
promise.then(function(payload) {
alert(payload);
});