无法使用 angularjs 获取从控制器中的服务加载的数据

unable to fetch data loaded from service in controller with angularjs

您好,我正在尝试在控制器中获取数据,该数据在路由更改时加载,服务已加载,但我无法在控制器中获取数据。它说未定义。感谢您的帮助。

var rajApp = angular.module('rajApp', []);
var resolver = function (access) {
    return {
        load: function ($q) {
            if (access) { // fire $routeChangeSuccess
                var deferred = $q.defer();
                deferred.resolve();
                return deferred.promise;
            } else { // fire $routeChangeError
                return $q.reject("/login");
            }
        }
    }
}
rajApp.config(['$routeProvider', '$controllerProvider',
    function ($routeProvider, $controllerProvider) {
        $routeProvider.
                when('/challan', {
                    templateUrl: 'templates/challan/challan.html',
                    controller: 'challanController',
                    resolve: {
                        getChallan: function (rajServices) {
                            return rajServices.getChallanDetails();
                        }
                    }

                }).
                otherwise({
                    redirectTo: '/login',
                    templateUrl: 'templates/login/index.html',
                    controller: 'loginController',
                    resolve: resolver(true)
                });

    }]).run(['$rootScope', '$location', '$routeParams', function ($rootScope, $location, $routeParams, $scope) {
        $rootScope.$on('$routeChangeStart', function (e, current, pre) {
            $rootScope.showCommonLoader = true;
        });
        $rootScope.$on('$routeChangeSuccess', function (e, current, pre) {
            $rootScope.showCommonLoader = false;
        });
        $rootScope.loadScript = function (url, type, charset) {
            if (type === undefined)
                type = 'text/javascript';
            if (url) {
                var script = document.querySelector("script[src*='" + url + "']");
                if (!script) {
                    var heads = document.getElementsByTagName("footer");
                    if (heads && heads.length) {
                        var head = heads[0];
                        if (head) {
                            script = document.createElement('script');
                            script.setAttribute('src', url);
                            script.setAttribute('type', type);
                            if (charset)
                                script.setAttribute('charset', charset);
                            head.appendChild(script);
                        }
                    }
                }
                return script;
            }
        };

        var scriptJS = {
            login: "js/login/login.js"
        },
        getRoutePath = $location.path(),
                spiltRoutePath = getRoutePath.split("/"),
                getRoute = spiltRoutePath[1],
                jsUrl = scriptJS[getRoute];

        // $rootScope.loadScript(jsUrl, 'text/javascript', 'utf-8');
    }]);

rajApp.service('rajServices', function rajServices($http, $q, $rootScope) {
    var mService = this;

    mService.getChallanDetails = function () {
        var defer = $q.defer();
        $http({
            method: 'POST',
            url: 'services/challan/get-challan-service.php'
        }).success(function (response) {
            defer.resolve(response);
        }).error(function (err, status) {
            defer.reject(err);
        })
        return defer.promise;
    }

    return mService;

});


rajApp.controller('loginController', function ($rootScope, $location, $routeParams, $scope, $http, $route, $window) {
    $scope.userName = '';
    $scope.showYearSelection = false;

    $scope.login = function () {
        $http({
            method: "POST",
            url: "services/login/login-authenticate.php?user=" + $scope.userName + "&password=" + $scope.password
        }).success(function (data) {
            if (data.success) {
                $scope.showYearSelection = true;
                $scope.yearCommand = data.yearCommand;
            }
        });
    }

    $scope.yearSelection = function () {
        $http({
            method: "POST",
            url: "services/year-selection/year-selection.php?year=" + $scope.yearSelect
        }).success(function (data) {
            if (data.success) {
                $location.path("/challan");
            }
        });
    }

});
rajApp.controller('challanController', ['$scope', 'rajServices',
    function ($rootScope, $location, $routeParams, $scope, $http, $route, $window, getChallan, $http) {
console.log(getChallan);
    }]);

您是否已经尝试 return 只在 getChallan 函数中承诺?

getChallan: function (rajServices) {
    return rajServices.getChallanDetails();
}

所以没有 then() 处理程序...

你的 challanController 也有问题,参数与数组中的字符串不匹配。修改如下:

rajApp.controller('challanController', function ($rootScope, $location, $routeParams, $scope, $http, $route, $window, getChallan) {
        console.log(getChallan);
    });

参见 https://docs.angularjs.org/tutorial/step_05(关于缩小的注释)