object不支持属性或方法getUserData

时间:2016-12-26 14:09:25

标签: angularjs angularjs-service

LoginController.js

var MyApp = angular.module('MyApp', []);
    MyApp.controller("LoginController",
            ["$scope", "$rootScope",
            function ($scope , dataService) {

            $scope.user = "sample";

            $scope.checkUser = function () {
                dataService.getUserData($scope.user).then(
                        function (results) {
                            $scope.userLoginInfo = results.userInfo;
                        },
                        function (results) {
                            $rootScope.showAlert(results, "There is a problem when trying to get user details.");
                        });
            };

        }]);

我的DataService.js

MyApp.factory("dataService",
    ["$http", "$rootScope", "$q",
    function ($http, $rootScope,$q) {

        var dataService = {};

        var getUserData = function (username) {

            var promise = $http.get(baseUrl()+"/Controllers/UserDetails/?username=" + username)
                .success(function (data, status, headers, config) {
                return data;
                })
                .error(function (data, status, headers, config) {
                    return data;
                });

            return promise;

        }

        return {
            getUserData: getUserData
        }

    }]);

我通过bundleconfig .on在登录控制器中调用dataService.getUserData包含了所有.js文件,发生了所描述的错误。

以下是堆栈跟踪

"TypeError: Object doesn't support property or method 'getUserData'\n   at $scope.checkUser (http://localhost:58949/app/Controllers/LoginController.js:41:9)
 at fn (Function code:2:195)\n   at expensiveCheckFn (http://localhost:58949/Scripts/angular.js:16123:11)\n   at callback (http://localhost:58949/Scripts/angular.js:26490:17)\n   at Scope.prototype.$eval (http://localhost:58949/Scripts/angular.js:17913:9)\n   at Scope.prototype.$apply (http://localhost:58949/Scripts/angular.js:18013:13)\n   at Anonymous function (http://localhost:58949/Scripts/angular.js:26495:17)\n   at n.event.dispatch (http://localhost:58949/Scripts/jquery-2.2.3.min.js:3:7481)\n   at r.handle (http://localhost:58949/Scripts/jquery-2.2.3.min.js:3:5547)"

非常感谢任何帮助

2 个答案:

答案 0 :(得分:1)

您没有在您的方案中注入dataService,而不是访问"dataService"它正在访问"$rootScope" 试试这个:

// controller

var MyApp = angular.module('MyApp', []);
MyApp.controller("LoginController", ["$scope", "$rootScope", "dataService",
    function($scope, $rootScope, dataService) {
        $scope.user = "sample";

        $scope.checkUser = function() {
            dataService.getUserData($scope.user).then(
                function(results) {
                    $scope.userLoginInfo = results.userInfo;
                },
                function(results) {
                    $rootScope.showAlert(results, "There is a problem when trying to get user details.");
                });
        };

    }
]);

同样在您的服务中

以下行/Controllers/UserDetails/?username=似乎不正确,我们按以下方式传递查询参数:

// "/Controllers/UserDetails?username="

// Service
MyApp.factory("dataService", ["$http", "$rootScope", "$q",
    function($http, $rootScope, $q) {

        var dataService = {};

        var getUserData = function(username) {

            var promise = $http.get(baseUrl() + "/Controllers/UserDetails?username=" + username)
                .success(function(data, status, headers, config) {
                    return data;
                })
                .error(function(data, status, headers, config) {
                    return data;
                });

            return promise;

        }

        return {
            getUserData: getUserData
        }

    }
]);

答案 1 :(得分:0)

注入dataService。

MyApp.controller("LoginController",
            ["$scope", "$rootScope","dataService",
            function ($scope ,rootScope dataService) {
. . 
}

如果dataService未正确注入,则会抛出错误。