通过angularjs服务$ http发布

时间:2015-06-17 06:48:27

标签: javascript jquery angularjs asp.net-web-api

我使用下面的代码。 dataService的内容是我的应用程序中可能的所有$ http。在控制器中,我使用此功能。该函数调用Web Api服务,调用此服务并返回正确的响应。在函数customerToggleSuccess中,数据为undefined。我不明白为什么。

(function () {
    angular.module('myApp')
        .factory('dataService', ['$q', '$http']);

    function dataService($q, $http,) {

        return {
            customerToggleActive: customerToggleActive
        };

        function customerToggleActive(customerId, index) {

            var customer = {
                "id": customerId,
                "index": index
            };

            return $http({
                method: 'POST',
                url: 'api/customer/validtoggle/',
                headers: {
                },
                transformResponse: function (data, headers) {
                },
                data: customer
            })
            .then(customerToggleData)
            .catch(customerToggleError)
        }

        function customerToggleData(response) {
            return response.data;
        }

        function customerToggleError(response) {
        }
    }

}());

(function () {

    angular.module('myApp')
        .controller('customerController', ['$scope', 'dataService', '$http', '$log', CustomerController]);

    function CustomerController($scope, dataService, $http, , $log) {
        var vm = this;

        vm.activeToggle = function (customerId, index) {

            dataService.customerToggleActive(customerId, index)
                .then(customerToggleSuccess)
                .catch(customerToggleCatch)
                .finally(customerToggleComplete);

            function customerToggleSuccess(data) {
                $log.info(data);
            }

            function customerToggleCatch(errorMsg) {
            }

            function customerToggleComplete() {
            }

        } 

    }

}());

2 个答案:

答案 0 :(得分:0)

只需返回$ http承诺,

  return $http({
             method: 'POST',
             url: 'api/customer/validtoggle/',
             headers: {
             },
             transformResponse: function (data, headers) {
             },
             data: customer
         })

您可以通过

访问它
dataService.customerToggleActive(customerId, index)
      .then(function (response) {
             // do your stuff
      })

或者,你可以做,

function dataService($q, $http,) { 
    var defer = $q.defer();
    ....
    ....
    $http({
         method: 'POST',
         url: 'api/customer/validtoggle/',
         headers: {
         },
         transformResponse: function (data, headers) {
         },
         data: customer
     })
     function customerToggleData(response) {
        defer.resolve (response.data);
    }

    function customerToggleError(response) {
        defer.reject(response);
    }
    return defer.promise;
}

答案 1 :(得分:0)

像这样,

(function () {
        angular.module('myApp')
            .factory('dataService', ['$q', '$http']);

        function dataService($q, $http,) {

            return {
                customerToggleActive: customerToggleActive
            };

            function customerToggleActive(customerId, index) {

                var customer = {
                    "id": customerId,
                    "index": index
                };

                return $http({
                    method: 'POST',
                    url: 'api/customer/validtoggle/',
                    headers: {
                    },
                    transformResponse: function (data, headers) {
                    },
                    data: customer
                })

            }      
        }

    }());