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

时间:2015-12-09 07:34:34

标签: angularjs service routes controllers

您好我正在尝试获取控制器中的数据,当路由更改时加载,服务被加载但我无法获取控制器中的数据。它说未定义。任何帮助表示赞赏。

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

1 个答案:

答案 0 :(得分:0)

您是否已尝试在getChallan函数中仅返回promise?

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(关于缩小的说明)