注入时AngularJS服务未定义

时间:2016-06-22 20:19:10

标签: javascript angularjs dependency-injection

Stackoverflow在这个标题下有很多问题,但是我正在浏览每个问题,而不是看到我正在做的有意义的不同。更重要的是,这不是我唯一的服务,其余的都可以工作。

我的服务:

(function () {    
    var envService = function ($location) {    
        //Removed           
        return {
            //Removed for SO, was simply an object of functions    
    }    
    var module = angular.module('passwordResetApp');
    module.factory('envService', ['$location', envService]);
}());

我的app.js:

(function () {
    'use strict';
    var app = angular.module('passwordResetApp', ['ngRoute', 'AdalAngular', 'ui.grid', 'ui.grid.pagination', 'ui.grid.edit', 'ui.grid.cellNav', 'ui.grid.selection']);
app
    .config([
            '$routeProvider', '$httpProvider', 'adalAuthenticationServiceProvider', '$locationProvider',
            function ($routeProvider, $httpProvider, adalProvider, $locationProvider) {
                $routeProvider
                    .when('/home',
                        {
                            templateUrl: '/app/views/home.html',
                            caseInsensitiveMatch: true
                        })
                    .when('/PasswordReset',
                        {
                            templateUrl: '/app/views/passwordReset.html',
                            controller: 'ResetRequestController as vm',
                            //requireAdLogin: true,
                            caseInsensitiveMatch: true
                        })
                    .when('/UserSearch',
                        {
                            templateUrl: '/app/views/userSearch.html',
                            controller: 'UserSearchController as vm',
                            //requireAdLogin: true,
                            caseInsensitiveMatch: true
                        })
                    .otherwise({ redirectTo: '/home' });
                adalProvider.init(
                    {
                        instance: 'https://login.microsoftonline.com/',
                        tenant: 'Removed for SO',
                        clientId: 'Removed for SO',
                        requireADLogin: false,
                        //anonymousEndpoints: [
                        //    '/'
                        //],
                        //endpoints: [
                        //    '/'
                        //],
                        extraQueryParameter: 'nux=1',
                        cacheLocation: 'localStorage',
                        // enable this for IE, as sessionStorage does not work for localhost.
                    },
                    $httpProvider
                );

                $locationProvider.html5Mode(true).hashPrefix('!');
            }
        ]
    );

app.directive('header',
    function() {
        return {
            //Attribute, not element
            restrict: 'A',
            replace: true,
            templateUrl: 'app/views/_header.html',
            controller: 'HeaderController as vm',
            caseInsensitiveMatch: true
        }
    });

app.directive('footer',
    function() {
        return {
            restrict: 'A',
            replace: true,
            templateUrl: 'app/views/_footer.html',
            caseInsensitiveMatch: true
        }
    });

app.run([
    '$route', '$http', '$rootScope', '$location',
    function ($route, $http, $rootScope) {
        $http.defaults.withCredentials = true;
        $rootScope.getUrlPath = function (url) {
            return baseUrl + url;
        };
    }
]);
}());

控制器尝试注入服务:

(function () {
    'use strict';    
    var app = angular.module('passwordResetApp');    
    var headerController = function ($scope, $location, envService, adalService) {
        var vm = this;
        vm.currentUser = {};
        vm.environment = envService.getEnvironment();

        vm.changeView = function(view) {
            $location.path(view);
        };

        vm.login = function () {
            adalService.login();
        };

        vm.logout = function () {
            adalService.logOut();
        };
    };

    app.controller('HeaderController', ['adalAuthenticationService', headerController]);    

}());

1 个答案:

答案 0 :(得分:2)

您必须在DI数组中注入所有依赖项,以便在控制器函数中使用它们。确保序列不会搞砸。

app.controller('HeaderController', ['$scope', '$location', 'envService', 'adalService', headerController]);