javascript AngularJs http get 响应未定义但数据存在于成功函数中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33406577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AngularJs http get response is undefined But the data is present inside success function
提问by Anandhakumar R
Am new to angular. I have tried to get the json response from http get method.
我是角度的新手。我试图从 http get 方法获取 json 响应。
My code is,
我的代码是,
app.factory('getdata', function ($http, $q){
this.getlist = function(){
alert('1');
return $http.get('http://www.w3schools.com/angular/customers.php',{'Access-Control-Allow-Origin': 'localhost:*'})
.then(function(response) {
console.log('response'); console.log(response.data); //I get the correct items, all seems ok here
alert('2');
return response.data;
});
}
return this;
});
/* Stream page controller */
app.controller('streamCtrl', function($scope,getdata, $ionicSlideBoxDelegate, $timeout, $ionicScrollDelegate, $location, $sce){
$scope.trustSrc = function(src) {
return $sce.trustAsResourceUrl(src);
}
getdata.getlist()
.then(function(arrItems){
$scope.sliders = arrItems;
});
alert($scope.sliders);
am getting the alert like 1, 'undefined' and 2 but the $scope.sliders has the data. because It was working once I resize the screen and also I can get correct alert inside the
我收到类似 1、'undefined' 和 2 的警报,但 $scope.slider 有数据。因为一旦我调整屏幕大小它就可以工作,而且我可以在里面得到正确的警报
getdata.getlist().then(function(arrItems) {
$scope.sliders = arrItems;
alert($scope.sliders);
});
回答by Sarjan Desai
The reason for getting alert is undefined
is because you haven't define $scope.sliders
in controller and http
request is async so alert($scope.sliders);
function call before getting data
from response and setting that data to $scope.sliders
.
获得警报的原因undefined
是因为您尚未$scope.sliders
在控制器中定义并且http
请求是异步的,因此alert($scope.sliders);
在data
从响应中获取并将该数据设置为$scope.sliders
.
You get alert($scope.sliders);
value only after getting success response.
alert($scope.sliders);
只有在获得成功响应后,您才能获得价值。
Other possibility is you can set $scope.sliders = {}
and then use alert($scope.sliders);
which show {}
in alert when then
function is called alert
shows actual response.
另一种可能性是您可以设置$scope.sliders = {}
然后使用alert($scope.sliders);
它{}
在then
调用函数时显示在警报中alert
显示实际响应。
Original plunkerbased on issue
基于问题的原始plunker
Check comments and updated code in new plunker
检查新plunker 中的注释和更新的代码
回答by Nidhish Krishnan
$http
is a asynchronous call, so what happens is that you have triggered a http call at line number 6while it gets the response the line number 10will be executed since all http
calls in angular are asynchronous.
$http
是一个异步调用,所以发生的情况是您在第 6 行触发了一个 http 调用,同时它获得了第 10 行的响应,因为http
angular 中的所有调用都是异步的。
That is why you are getting undefined
这就是为什么你得到 undefined
If you want to do anything after the response just do the stuff within the http promise ie. within the .then
function block like as shown below
如果你想在响应之后做任何事情,只需在 http 承诺中做一些事情,即。在.then
功能块内,如下所示
getdata.getlist()
.then(function(response){ // http promise block
$scope.sliders = response.data;
console.log($scope.sliders);
});