离子工厂返回空对象

时间:2017-02-18 23:41:54

标签: javascript angularjs ionic-framework

我可能会尝试一些不应该的东西。 仍然没有把头包裹在整个" Singleton"的事情。

问题是,我有一个工厂支持从API获取一些json数据。 我从工厂回来了一个空对象。

非常感谢任何帮助。

.factory('specialities', function($http){
    var specialities = [];
    return {
        getspecialities: function(){
            $http({
                method: 'GET',
                url: "https://mydomain.abc/Api/specialities",
                headers: {'Content-Type': 'application/x-www-form-urlencoded'}
            }).then(function successCallback(response) {    
                specialities = response.data.list;
            }, function errorCallback(response) {

            });
            return specialities;
        }  
    }               
})

这是我的控制器。

.controller('loadingCtrl', ['$scope', '$stateParams', 'specialities', 'areas', '$timeout',
function ($scope, $stateParams, specialities, areas, $timeout) {
    $scope.specialities = specialities.getspecialities();
    $scope.areas = areas.getareas();
    //console.log($scope.areas);
     $scope.slide = function(){
         console.log($scope.specialities);
         console.log($scope.areas);
     }
     $timeout($scope.slide, 5000);
}])

即使超时后,我得到的只是控制台中的空数组。 请帮忙。

谢谢

----编辑 我正在尝试以下代码。 控制器

.controller('loadingCtrl', ['$scope', '$stateParams', 'specialities', '$timeout', '$state',
function ($scope, $stateParams, specialities,  $timeout, $state) {  
    specialities.getspecialities().then(function(specialities)
    {
       $scope.specialities = specialities;
        console.log($scope.specialities);
    });
}])

.factory('specialities', function($http, $q){
    return {
         getspecialities: function(){
            return $http({
                method: 'GET',
                url: "https://mydomain.abc/Api/specialities",
                headers: {'Content-Type': 'application/x-www-form-urlencoded'}
            }).then(function successCallback(response) {    
                return response.data.list;
            });
        } 
    }               
})

现在控制台永远不会记录,并且似乎没有调用工厂,因为没有调用API网址。

2 个答案:

答案 0 :(得分:0)

您的$http电话是异步电话,并返回承诺。

您的return specialities$http电话启动后立即执行,但在实际完成工作之前执行。

更改getspecialities函数以实际返回$http(这是一个Promise)的返回值,并在调用它时使用该promise。

服务:

.factory('specialities', function($http, $q){
    return {
        getspecialities: function(){
            return $http({
                method: 'GET',
                url: "https://mydomain.abc/Api/specialities",
                headers: {'Content-Type': 'application/x-www-form-urlencoded'}
            }).then(function successCallback(response) {    
                return response.data.list;
            };
        }  
    }               
})

控制器:

.controller('loadingCtrl', ['$scope', '$stateParams', 'specialities', 'areas', '$timeout',
function ($scope, $stateParams, specialities, areas, $timeout) {
    specialities.getspecialities().then(function(specialities)
    {
       $scope.specialities = specialities;
       ... any other stuff that needs to happen when you receive the value of specialities ...
    });
    ...

}])

答案 1 :(得分:0)

试试这个

控制器

.controller('loadingCtrl', ['$scope', '$stateParams', 'specialities', '$timeout', '$state',
function ($scope, $stateParams, specialities,  $timeout, $state) {  
    specialities.getspecialities(function(specialities)
    {
       $scope.specialities = specialities;
        console.log($scope.specialities);
    });
}]);

服务

.factory('specialities', function($http, $q){
    return {
         getspecialities: function(callback){
            return $http({
                method: 'GET',
                url: "https://mydomain.abc/Api/specialities",
                headers: {'Content-Type': 'application/x-www-form-urlencoded'}
            }).then(function successCallback(response) {    
                return callback(response.data.list);
            });
        } 
    }               
})