从服务$ HTTP数据到控制器 [英] $http data from service to controller

查看:93
本文介绍了从服务$ HTTP数据到控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一种方法来从我的服务传送所请求的数据到控制器亚姆搜索。一个简单的返回数据不工作....我想知道为什么?

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('DataService', function($http, $log) {

    return {

        getEmployees: function( ) {

            $http({method: 'GET', url: 'php/data.php'})
                .success ( function ( data, status, header, config ){

                    //return data

                })
                .error ( function ( data, status, header, config ){

                    $log.log ( status );

                })

        },

        getTest: function( ) {

            return "test123";

        }

    };

});


test.controller('employees', function ( $scope, DataService ) {

    $scope.test = DataService.getEmployees();

});

感谢您。罗伯特

推荐答案

您可以使用 <强> $ q 和承诺。 $ HTTP 是一个异步调用

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

工厂

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";
        }
   };
});

控制器

DataService.getEmployees().then(function(data){
    $scope.test = data;
});

这篇关于从服务$ HTTP数据到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆