TypeError:historyService.getHistory(...)。success不是函数

时间:2015-12-10 16:01:58

标签: angularjs ionic

正如您所看到的,我正在尝试获取订单历史记录并将其注入我的视图但我收到错误

  

TypeError:historyService.getHistory(...)。success不是函数

不知道问题是什么,但每当我从我的控制器中移除代码时它工作正常,还值得一提的是我在控制台中获取了nav.service和order history.service的内容。

historyService.getHistory()
        .success(function (data, status, headers, config){
            vm.historyItems = data;
        });

History.Controller:

    (function () {
            'use strict';

            angular
                .module('app')
                .controller('HistoryController', HistoryController);

            HistoryController.$inject = ['navigationService','historyService', '$scope', '$timeout', 'ionicMaterialInk', 'ionicMaterialMotion'];
            function HistoryController(navigationService, historyService, $scope, $timeout, ionicMaterialInk, ionicMaterialMotion) {
                // Set Header
                $scope.$parent.$parent.$parent.showHeader();
                $scope.$parent.$parent.$parent.clearFabs();
                $scope.$parent.$parent.$parent.setExpanded(false);
                $scope.$parent.$parent.$parent.setHeaderFab(false);


                // Delay expansion
                $timeout(function () {

                    ionicMaterialMotion.slideUp({
                        selector: '.slide-up'
                    });

                }, 300);

                var vm = this;
                vm.menuItems = [];
                vm.historyItems = [];

                navigationService.getAllNavigations()
                .success(function (data, status, headers, config){
                    vm.menuItems = data;
                    $timeout(function () {
                        // Set Motion
                        ionicMaterialMotion.fadeSlideInRight();

                        // Set Ink

                        ionicMaterialInk.displayEffect();
                    }, 100);
                });

                historyService.getHistory()
                .success(function (data, status, headers, config){
                    vm.historyItems = data;
                });



            };
        })();

History.Service:

(function () {
    'use strict';
    angular
        .module('app')
        .service('historyService', historyService);

    historyService.$inject = ['$http', 'restServer', '$q'];
    function historyService($http, restServer, $q) {
        var historyService = {
            getHistory: getHistory
        };

        return historyService;

        function getHistory(OrderId) {

            var deferred = $q.defer();

            if (OrderId != undefined)
                //Order ID exist
                $http.get(restServer + '/order_history/' + OrderId)
                    .success(function (data, status, headers, config) {
                        console.log('getHistory(' + OrderId + ')');
                        console.log(data);

                    })
                    .error(function (data, status, headers, config) {
                        console.log('getHistory(' + OrderId + ') error');
                        console.log(data);

                    });
            else
                $http.get(restServer + '/order_history/')
                    .success(function (data, status, headers, config) {
                        console.log('getHistory: ');
                        console.log(data);
                    })
                    .error(function (data, status, headers, config) {
                        console.log('getHistory error');
                        console.log(data);
                    });

            return deferred.promise;
        }

    };
})();

2 个答案:

答案 0 :(得分:1)

检查docs for the $q Promise API,这些是promise对象上可用的方法:

  • then(successCallback, errorCallback, notifyCallback)
  • catch(errorCallback)
  • finally(callback, notifyCallback)

您需要的是then

success仅适用于$http承诺,并且已弃用,应使用标准then方法替换。请参阅Deprecation Notice in $http docs

编辑:这可能会修复该错误,但您在服务中缺少更多代码。您应该直接返回$http.get()调用返回的promise对象,否则您需要在某个阶段调用deferred.resolve(),以便完成承诺 e.g。

.success(function (data, status, headers, config) {
    deferred.resolve(data);
})

答案 1 :(得分:0)

在之前的History.Service中

$http.get(restServer + '/order_history/')

我忘了添加回复:

return $http.get(restServer + '/order_history/')