$http data from service to controller

后端 未结 1 1460
执笔经年
执笔经年 2021-01-26 15:57

iam searching for a way to transfer the requested data from my service to the controller. a simple return data dont work....i wondering why?

test.factory(\'DataS         


        
相关标签:
1条回答
  • 2021-01-26 16:12

    You can use $q and promise. $http is an asynchronous call

    factory

    test.factory('DataService', function($http, $log, $q) {
        return {
            getEmployees: function( ) {
                var d = $q.defer();
                $http({method: 'GET', url: 'php/data.php'})
                    .success(function(data, status, header, config){
                        d.resolve(data);
                    }.error(function(error){
                        d.reject(error);
                    });
                return d.promise;
            },
            getTest: function( ) {
                return "test123";
            }
       };
    });
    

    Controller

    DataService.getEmployees().then(function(data){
        $scope.test = data;
    });
    
    0 讨论(0)
提交回复
热议问题