角度喷射器错误将工厂注入控制器

时间:2016-05-20 13:52:03

标签: angularjs

我在clientService上遇到了一个注入器错误,无法找出原因。我尝试创建一个clientsService并将其传递给clientsController,但它在Chrome工具中出现此错误。我对Angular很新,所以任何帮助都会非常感激,因为我需要尽快启动并运行这个项目。

angular.js:13003 Error: [$injector:unpr] Unknown provider: clientsServiceProvider <- clientsService <- clientsController
http://errors.angularjs.org/1.5.0-rc.0/$injector/unpr?p0=clientsServiceProvider%20%3C-%20clientsService%20%3C-%20clientsController
    at angular.js:68
    at angular.js:4463
  `enter code here`  at Object.getService [as get] (angular.js:4616)
    at angular.js:4468
    at getService (angular.js:4616)
    at Object.invoke (angular.js:4648)
    at extend.instance (angular.js:9622)
    at nodeLinkFn (angular.js:8731)
    at compositeLinkFn (angular.js:8035)
    at publicLinkFn (angular.js:7915)


Here are the included file listings below
// app.js
// clientsController.js
// clientsService.js


////////////
// app.js //
////////////
var app = angular.module('app',
[
    'auth0',
    'ngRoute',
    'angular-storage',
    'angular-jwt',
    'scope'
]);

app.config(function myAppConfig($routeProvider,
    authProvider,
    $httpProvider,
    $locationProvider,
    jwtInterceptorProvider) {
    $routeProvider
        .when('/',
        {
            controller: 'clientsController',
            templateUrl: 'views/clients/clients.html',
            pageTitle: 'Clients',
            requiresLogin: true
        })
        .when('/login',
        {
            controller: 'loginController',
            templateUrl: 'views/login.html',
            pageTitle: 'Login'
        })
        .when('/clients',
        {
            controller: 'clientsController',
            templateUrl: 'views/clients/clients.html',
            pageTitle: 'Clients',
            requiresLogin: true
        });

    authProvider.init({
        domain: AUTH0_DOMAIN,
        clientID: AUTH0_CLIENT_ID,
        loginUrl: '/login'
    });

    authProvider.on('loginSuccess',
        function($location, profilePromise, idToken, store) {
            console.log("Login Success");
            profilePromise.then(function(profile) {
                store.set('profile', profile);
                store.set('token', idToken);
            });
            $location.path('/');
        });

    authProvider.on('loginFailure',
        function() {
            alert("Error");
        });

    authProvider.on('authenticated',
        function($location) {
            console.log("Authenticated");

        });

    jwtInterceptorProvider.tokenGetter = function(store) {
        return store.get('token');
    }

    // Add a simple interceptor that will fetch all requests and add the jwt token to its authorization header.
    // NOTE: in case you are calling APIs which expect a token signed with a different secret, you might
    // want to check the delegation-token example
    $httpProvider.interceptors.push('jwtInterceptor');
});

app.run(function($rootScope, auth, store, jwtHelper, $location) {
    $rootScope.$on('$locationChangeStart',
        function() {

            var token = store.get('token');
            if (token) {
                if (!jwtHelper.isTokenExpired(token)) {
                    if (!auth.isAuthenticated) {
                        auth.authenticate(store.get('profile'), token);
                    }
                } else {
                    // Either show the login page or use the refresh token to get a new idToken
                    $location.path('/');
                }
            }

        });
});

//////////////////////////
// clientsController.js //
//////////////////////////

angular.module('app').controller('clientsController', function ($scope, auth, $http, $location, $timeout,clientsService) {

    var vm = this;
    vm.auth = auth;
    vm.appTitle = "Ortho CRM";

    vm.loadClientsSummary = function() {
            clientsService.getAllClientsSummary(vm.currentPage - 1, vm.pageSize)
                .then(function(data) {
                        vm.totalRecords = data.totalRecords;
                        vm.clients = data.results;
                        vm.filteredClients = data.results;

                    },
                    function(error) {
                        $window.alert('Sorry, an error occurred: ' + error.data.message);
                    });
    }

    vm.loadClientById = function(id) {
        for (var i = 0; i < vm.clients.length; i++) {
            var cli = vm.clients[i];
            if (cli.id === id) {
                return cli;
            }
        }
        return null;
    }

    vm.init = function() {
        alert("Init");
        vm.loadClientsSummary();
    }

    vm.init();

});

////////////////////
// clientsService //
////////////////////

angular.module('app').factory('clientsService',function($http, $q) {

        var serviceBase = '/api/clientsService/',

        factory = {};

        //////////////
        // Clients //
        /////////////

        // Creates

        factory.insertClient = function(client) {
            return $http.post(serviceBase + 'postClient', client)
                .then(function(results) {
                    client.id = results.data.id;
                    return results.data;
                });
        };

        // Reads

        factory.getAllClientsSummary = function(pageIndex, pageSize) {
            return getPagedResource('ApiClientsSummary', pageIndex, pageSize);
        };

        factory.getClientById = function(id) {
            //then does not unwrap data so must go through .data property
            //success unwraps data automatically (no need to call .data property)
            return $http.get(serviceBase + 'GetClientById/' + id);
        };

        factory.getLeadStrengthValuesByClientId = function(id) {
            //then does not unwrap data so must go through .data property
            //success unwraps data automatically (no need to call .data property)
            return $http.get(serviceBase + 'GetLeadStrengthValuesByClientId/' + id);
        };

        factory.getActiveClientLeadStatusByClientId = function(id) {
            return $http.get(serviceBase + 'GetActiveClientLeadStatusByClientId/' + id);
        }

        // Updates

        factory.updateClient = function(client) {
            return $http.put(serviceBase + 'putClient/' + client.id, client)
                .then(function(status) {
                    return status.data;
                });
        };

        // Deletes

        factory.deleteClient = function(id) {
            return $http.delete(serviceBase + 'deleteClient/' + id)
                .then(function(status) {
                    return status.data;
                });
        };

        //////////////
        // Contacts //
        //////////////

        // Creates

        // Reads

        factory.getClientContactByClientId = function(id) {
            //then does not unwrap data so must go through .data property
            //success unwraps data automatically (no need to call .data property)
            return $http.get(serviceBase + 'GetClientContactByClientId/' + id);
        };

        factory.getClientContactSummaryByClientId = function(id) {
            return $http.get(serviceBase + 'GetClientContactSummaryByClientId/' + id);
        };

        factory.getClientChampionContactByClientId = function(id) {
            //then does not unwrap data so must go through .data property
            //success unwraps data automatically (no need to call .data property)
            return $http.get(serviceBase + 'GetClientChampionContactByClientId/' + id);
        };

        // Updates
        factory.updateContact = function(contact) {
            return $http.put(serviceBase + 'updateContact/' + contact.clientId, contact)
                .then(function(status) {
                    return status.data;
                });
        };

        // Deletes


        ////////////
        // States //
        ////////////

        // Creates

        // Reads

        factory.getAllStates = function() {
            return $http.get(serviceBase + 'getAllStates')
                .then(
                    function(results) {
                        return results.data;
                    });
        };

        factory.getStateById = function(id) {
            return $http.get(serviceBase + 'getStateById/' + id)
                .then(
                    function(results) {
                        return results.data;
                    });
        };

        ////////////////
        // Phone Type //
        ////////////////

        // Creates

        // Reads

        factory.getAllPhoneTypes = function() {
            return $http.get(serviceBase + 'getAllPhoneTypes')
                .then(
                    function(results) {
                        return results.data;
                    });
        };

        factory.getPhoneTypeById = function(id) {
            return $http.get(serviceBase + 'getPhoneTypeById/' + id)
                .then(
                    function(results) {
                        return results.data;
                    });
        };

        ////////////////////////
        // CellPhoneProviders //
        ////////////////////////

        // Creates

        // Reads

        factory.getAllCellPhoneProviders = function() {
            return $http.get(serviceBase + 'getAllCellPhoneProviders')
                .then(
                    function(results) {
                        return results.data;
                    });
        };

        factory.getCellPhoneProviderById = function(id) {
            return $http.get(serviceBase + 'getCellPhoneProviderById/' + id)
                .then(
                    function(results) {
                        return results.data;
                    });
        };

        ////////////////////
        // ContactMethods //
        ////////////////////

        // Creates

        // Reads

        factory.getAllContactMethods = function() {
            return $http.get(serviceBase + 'getAllContactMethods')
                .then(
                    function(results) {
                        return results.data;
                    });
        };

        factory.getContactMethodById = function(id) {
            return $http.get(serviceBase + 'getContactMethodById/' + id)
                .then(
                    function(results) {
                        return results.data;
                    });
        };

        /////////////////
        // ClientTypes //
        /////////////////

        // Creates

        // Reads

        factory.getAllClientTypes = function() {
            return $http.get(serviceBase + 'getAllClientTypes')
                .then(
                    function(results) {
                        return results.data;
                    });
        };

        factory.getClientTypeById = function(id) {
            return $http.get(serviceBase + 'getClientTypeById/' + id)
                .then(
                    function(results) {
                        return results.data;
                    });
        };


        //////////////////////////
        // Client Lead Statuses //
        //////////////////////////

        // Creates

        // Reads

        factory.getAllClientLeadStatuses = function() {
            return $http.get(serviceBase + 'GetAllClientLeadStatuses');
        }

        factory.getClientLeadStatusById = function(id) {
            return $http.get(serviceBase + 'getClientLeadStatusById/' + id)
                .then(
                    function(results) {
                        return results.data;
                    });
        };

        factory.checkUniqueValue = function(id, property, value) {
            if (!id) id = 0;
            return $http.get(serviceBase + 'checkUnique/' + id + '?property=' + property + '&value=' + escape(value))
                .then(
                    function(results) {
                        return results.data.status;
                    });
        };

        function getPagedResource(baseResource, pageIndex, pageSize) {
            var resource = baseResource;
            resource += (arguments.length == 3) ? buildPagingUri(pageIndex, pageSize) : '';
            return $http.get(serviceBase + resource)
                .then(function(response) {
                    var clients = response.data;
                    //extendCustomers(custs);
                    return {
                        totalRecords: parseInt(response.headers('X-InlineCount')),
                        results: clients
                    };
                });
        }

        function buildPagingUri(pageIndex, pageSize) {
            var uri = '?$top=' + pageSize + '&$skip=' + (pageIndex * pageSize);
            return uri;
        }

        return factory;
    });

3 个答案:

答案 0 :(得分:1)

您必须将服务注入控制器。

angular.module('clientsController', ['clientsService']);

答案 1 :(得分:1)

您可能没有在clientsService.js文件中加入index.html

您需要在html中包含它(并在app.js脚本之前)。

例如

<script src="js/clientsService.js" type='text/javascript'/> <script src="js/app.js" type='text/javascript'/>

答案 2 :(得分:0)

  1. 是的,如果您的视图使用“ng-strict”指令,则问题将发生在crome中,这意味着应该正确声明并注入所有自定义注入(非角度内置)。如果是这样,那么尝试显式注入。像这样:

    app.controller("clientsController", clientsController);
    
    clientsController.$inject = ["clientsService", "$scope", "auth", "$http","$location", "$timeout"];
    
    function clientsController(clientsService, $scope, auth, $http, $location, $timeout)
    {
      // Controller functions
    }
    
  2. 不确定,如果您的应用程序是在MVC中构建的?但是,请尝试检查您是否已将“customerService.js”添加并注册为AppStart的BundleConfig中的ScriptBundle。