【发布时间】:2015-10-29 19:29:26
【问题描述】:
我是 Angular 新手。我试图从 http get 方法获取 json 响应。
我的代码是,
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);
收到类似 1、“未定义”和 2 的警报 但 $scope.sliders 有数据。因为一旦我调整屏幕大小,它就可以工作,而且我可以在
中获得正确的警报getdata.getlist().then(function(arrItems) {
$scope.sliders = arrItems;
alert($scope.sliders);
});
【问题讨论】:
标签: javascript angularjs