【发布时间】:2016-07-22 13:12:40
【问题描述】:
我正在使用 Ionic Framework 7 AngularJS 构建应用程序
$ ionic info
Your system information:
Cordova CLI: 6.2.0
Gulp version: CLI version 3.9.1
Gulp local:
Ionic Framework Version: 1.2.4
Ionic CLI Version: 1.7.16
Ionic App Lib Version: 0.7.3
OS: Distributor ID: LinuxMint Description: Linux Mint 17.1 Rebecca
Node Version: v0.12.2
我正在构建的页面检索教堂服务列表并将它们显示在主详细信息列表中。所以我按照以下代码将 $http 代码放入服务中:
services.js:
.service('ServicesService', ['$http', '$sce', function($http, $sce){
var services = [];
$http.get('http://demo0194057.mockable.io/services').then(function(response) {
services = response.data;
window.q = services; //checking value of services var
}, function(error) {
console.error("Error loading Services Endpoint! " + error);
});
return {
getAllServices: function() {
console.log('from inside return obj ' + services);
window.p = services; // checking value of services var
return services;
}
}
}])
controller.js:
.controller('servicesCtrl', function($scope, $http, ServicesService, InfoService) {
$scope.services = [];
$scope.services = ServicesService.getAllServices();
window.r = $scope.services;
$scope.doRefresh = function() {
$http.get('http://demo0194057.mockable.io/services').success(function(response) {
$scope.services = response;
})
.finally(function() {
// stop the ion-refresher from spinning
$scope.$broadcast('scroll.refreshComplete');
});
}
})
所以我的问题是服务返回一个空数组,而不是来自 JSON REST API 的对象数组。我在代码中放入了一些调试变量,我可以从控制器中看到,服务正在返回一个空数组(var window.r)。在服务中,window.p 也是空的,但是 window.q 具有正确的对象数据,这意味着 API 调用工作正常。我无法弄清楚数据在哪里丢失,因此无法从服务中成功返回。
请帮忙
【问题讨论】:
标签: javascript angularjs ionic-framework