【问题标题】:unable to fetch data loaded from service in controller with angularjs无法使用 angularjs 获取从控制器中的服务加载的数据
【发布时间】:2015-12-09 07:34:34
【问题描述】:

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

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

【问题讨论】:

  • 为什么你定义了两次服务'rajServices'?一旦它被定义为工厂,那么它就被定义为(同名)服务。
  • 很抱歉这是个错误,但您能帮我获取数据吗?
  • 我会尝试...但是您的代码相当不完整,如果没有 jsfiddle 或 plunker 就很难复制问题
  • 分享凭证不是个好主意...在这里!但是我正在尝试您的代码
  • 它是我的测试服务器,所以不用担心

标签: angularjs service routes controllers


【解决方案1】:

您是否已经尝试在 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(关于缩小的说明)

【讨论】:

  • 你能帮我吗?
  • 如果没有直接编辑代码的可能性,调试应用程序并不容易......这就是为什么 stackoverflow 上的大多数人都需要 plunker 或 jsfiddle 的原因。
  • 哦,我没用过
  • 我修改了答案:你的 challanController 有问题
  • 太棒了!!!!!!它像魅力一样工作,只有一个错误错误:未知提供者:getChallanProvider
猜你喜欢
  • 2016-04-29
  • 2015-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-09
  • 1970-01-01
  • 1970-01-01
  • 2015-02-17
相关资源
最近更新 更多