Angularjs - 工厂未定义

时间:2016-02-12 09:23:54

标签: angularjs asp.net-mvc angularjs-factory

我正在使用MVC和angularjs开发一个应用程序,并且是angularjs的新应用程序。代码使用了在不同的js文件中定义的大量不同工厂,并且这些工厂在模块中正确注入,但我仍然看到“工厂未定义错误”

app.js如下: -

(function () {
          'use strict';

         angular.module('MyApp', ['common.core', 'common.ui'])
           .run(run);

          run.$inject = ['$rootScope', '$location', '$cookieStore',         '$http','membershipService'];

        function run($rootScope, $location, $cookieStore, $http) {
            // handle page refreshes
            $rootScope.repository = $cookieStore.get('repository') || {};
            if ($rootScope.repository.loggedUser) {
                $http.defaults.headers.common['Authorization'] = $rootScope.repository.loggedUser.authdata;
            }
        }
    })();

工厂在单独的文件中定义,如下所示: -

 (function (app) {
        'use strict';

        var app = angular.module('MyApp');
        app.factory('membershipService', membershipService);

        membershipService.$inject = ['apiService', 'notificationService','$http', '$base64', '$cookieStore', '$rootScope'];

        function membershipService(apiService, notificationService, $http, $base64, $cookieStore, $rootScope) {

            var service = {
                login: login,
                register: register,
                saveCredentials: saveCredentials,
                removeCredentials: removeCredentials,
                isUserLoggedIn: isUserLoggedIn
            }

            function login(user, completed) {
                apiService.post('/api/account/authenticate', user,
                completed,
                loginFailed);
            }

            function register(user, completed) {
                apiService.post('/signup', user,
                completed,
                registrationFailed);
            }

            function saveCredentials(user) {
                var membershipData = $base64.encode(user.username + ':' + user.password);

                $rootScope.repository = {
                    loggedUser: {
                        username: user.username,
                        authdata: membershipData
                    }
                };

                $http.defaults.headers.common['Authorization'] = 'Basic ' + membershipData;
                $cookieStore.put('repository', $rootScope.repository);
            }

            function removeCredentials() {
                $rootScope.repository = {};
                $cookieStore.remove('repository');
                $http.defaults.headers.common.Authorization = '';
            };

            function loginFailed(response) {
                notificationService.displayError(response.data);
            }

            function registrationFailed(response) {

                notificationService.displayError('Registration failed. Try again.');
            }

            function isUserLoggedIn() {
                return $rootScope.repository.loggedUser != null;
            }

            return service;
        }
    })(angular.module('common.core'));

控制器的定义如下: -

(function (app) {
    'use strict';
    var app = angular.module('MyApp');
    app.controller('SignUpController', SignUpController);

    SignUpController.$inject = ['$scope', 'membershipService', 'notificationService', '$rootScope', '$location'];

      function SignUpController($scope, membershipService, notificationService, $rootScope, $location) {
            $scope.pageClass = 'page-login';
            $scope.register = register;
            $scope.user = {};
        };

        function register() {
            membershipService.register($scope.user, registerCompleted)
        }

        function registerCompleted(result) {
            if (result.data.success) {
                membershipService.saveCredentials($scope.user);
                notificationService.displaySuccess('Hello ' + $scope.user.username);
                $scope.userData.displayUserInfo();
                $location.path('/');
            }
            else {
                notificationService.displayError('Registration failed. Try again.');
            }
        }
    })(angular.module('common.core'));   

脚本加载如下: -

bundles.Add(new ScriptBundle("~/bundles/Controllers").Include(
              "~/Scripts/Controllers/app.js",
              "~/Scripts/Services/apiService.js",
              "~/Scripts/Services/notificationService.js",
              "~/Scripts/Services/membershipService.js",
              "~/Scripts/Services/fileUploadService.js",
              "~/Scripts/Controllers/SignUpController.js"

'register'函数抛出错误'membershipserservice not defined',我做错了什么?请帮忙。

1 个答案:

答案 0 :(得分:2)

我看到了这个:

run.$inject = ['$rootScope', '$location', '$cookieStore', '$http','membershipService'];

 function run($rootScope, $location, $cookieStore, $http) {
       // handle page refreshes

您缺少函数参数列表中的membershipService

还有:

    SignUpController.$inject = ['$scope', 'membershipService', 'notificationService', '$rootScope', '$location'];

    function SignUpController($scope, membershipService, notificationService, $rootScope, $location) {
        $scope.pageClass = 'page-login';
        $scope.register = register;
        $scope.user = {};
    };

    function register() {
        membershipService.register($scope.user, registerCompleted)
    }

    function registerCompleted(result) {
        if (result.data.success) {
            membershipService.saveCredentials($scope.user);
            notificationService.displaySuccess('Hello ' + $scope.user.username);
            $scope.userData.displayUserInfo();
            $location.path('/');
        }
        else {
            notificationService.displayError('Registration failed. Try again.');
        }
    }

这将永远不会起作用,因为您在函数之外定义了将membershipService传递到的函数。

SignUpController应该类似于:

        function SignUpController($scope, membershipService, notificationService, $rootScope, $location) {
            $scope.pageClass = 'page-login';
            $scope.register = register;
            $scope.user = {};

            function register() {
                membershipService.register($scope.user, registerCompleted)
            }

            function registerCompleted(result) {
                if (result.data.success) {
                    membershipService.saveCredentials($scope.user);
                    notificationService.displaySuccess('Hello ' + $scope.user.username);
                    $scope.userData.displayUserInfo();
                    $location.path('/');
                }
                else {
                    notificationService.displayError('Registration failed. Try again.');
                }
            }
        };

请参阅方法SignUpController,即DI将注入参数的位置。

但是,您已经定义了需要这些参数的函数在SignUpController函数的范围之外