Angularjs 使用“Http”时,工厂中的Http函数在控制器中不起作用;然后";

Angularjs 使用“Http”时,工厂中的Http函数在控制器中不起作用;然后";,angularjs,Angularjs,我试图在控制器中调用工厂的http函数,当我使用时,它不起作用 我的代码如下: 模块: var brandlistmodule=angular.module('newApp')) 工厂: brandlistmodule.factory('BrandListFactory',["$http","$routeParams",function($http,$routeParams){ console.log(2) return { a: function(respons

我试图在控制器中调用工厂的
http
函数,当我使用
时,它不起作用
我的代码如下:

模块:

var brandlistmodule=angular.module('newApp
'))

工厂:

brandlistmodule.factory('BrandListFactory',["$http","$routeParams",function($http,$routeParams){
    console.log(2)
    return {
        a: function(response){
            console.log('1');
             return $http.get("/restful_api/category_1/?format=json").then(function(response){
                 console.log('3');
                  return response.data;
             });

        },
        b: function(response){
             return $http.get("/restful_api/category_refer_a/?format=json").then(function(response){
                  return response.data;
             });

        },
        c: function(response){
             return $http.get("/restful_api/brand_test_search/?format=json").then(function(response){
                  result = response.data
                  return result;
             });

        },

    }

}])
控制器:

brandlistmodule.controller('brandlistCtrl', ['BrandListFactory','$scope','$rootScope','$http',function (BrandListFactory,$scope,$rootScope,$http) {
        $scope.$on('$viewContentLoaded', function () {
        $rootScope.category = function(){
         BrandListFactory.a.success(function(data){
             console.log('9');
             $rootScope.category =data 
         });

     };
在控制台中,它只能显示“2”,如果我按如下方式更改控制器,它将正常工作

brandlistmodule.controller('brandlistCtrl', ['BrandListFactory','$scope',function (BrandListFactory,$scope) {
    $scope.$on('$viewContentLoaded', function () {
   BrandListFactory.BrandCategoryList()

我建议创建一个服务,负责所有http调用

.service('HttpService', ['$rootScope', '$http', 'Ls', 'CommonService', 'DateService', function ($rootScope, $http, Ls, CommonService, DateService) {
        return {
            CallService: function (url, callback) {                   
                $http.get(url)
                    .success(function (data, status) {                            
                        callback(data, status);
                    }).error(function (data, status) {
                        callback(data, status);
                    });                 
            } 
        }
    });
如果将参数回调传递到
HttpService.CallService
中,则可以在
http.get
调用的
success
error
函数中调用回调

例如,在控制器中,您可以执行以下操作:

    HttpService.CallService('/restful_api/category_refer_a/?format=json', function (data) {
                        console.log(data)
                        //do something with your data

    });
因此,在您的示例中,您可以:

 $scope.$on('$viewContentLoaded', function () {
             HttpService.CallService('/restful_api/category_refer_a/?format=json', function (data) {
                            console.log('9');
                 $rootScope.category =data 

        });
});
或者你干脆把电话改成

b: function(callback){
             return $http.get("/restful_api/category_refer_a/?format=json").then(function(response){
                  callback(response.data);
             });

        },


希望这对您有所帮助

您的问题是您正在工厂内调用Then(),因此您可以做的是:

1-那就不要打电话了。。但是将所有$http请求返回给工厂的调用方。。比如:

brandlistmodule.factory('BrandListFactory',["$http","$routeParams",function($http,$routeParams){
    console.log(2)
    return {
        a: function(response){
            console.log('1');
             return $http.get("/restful_api/category_1/?format=json");

        },
        b: function(response){
             return $http.get("/restful_api/category_refer_a/?format=json");

        },
        c: function(response){
             return $http.get("/restful_api/brand_test_search/?format=json");

        },

    }

}])
2-使用$q,自己制作asyncron链。。比如:

brandlistmodule.factory('BrandListFactory',["$http","$routeParams","$q",function($http,$routeParams,$q){
    console.log(2)
    return {
        a: function(response){
var deferred = $q.defer();
            console.log('1');
             return $http.get("/restful_api/category_1/?format=json").then(function(response){
                 console.log('3');
                 deferred.resolve( response.data);
             }).catch(function(err){
deferred.reject(err);
});

return deffered.promise;

        } 
// and same for others
    }

}])

是的,你告诉我为什么它不能工作:在工厂里,你更喜欢使用“return”而不是“then”,如果你坚持使用“then”,你应该使用$q来解决可能的异步问题,对吗?我非常感谢你耐心的解释,我已经在你的帮助下解决了这个问题。谢谢
brandlistmodule.factory('BrandListFactory',["$http","$routeParams","$q",function($http,$routeParams,$q){
    console.log(2)
    return {
        a: function(response){
var deferred = $q.defer();
            console.log('1');
             return $http.get("/restful_api/category_1/?format=json").then(function(response){
                 console.log('3');
                 deferred.resolve( response.data);
             }).catch(function(err){
deferred.reject(err);
});

return deffered.promise;

        } 
// and same for others
    }

}])