angular js Custom Service/factory unable to inject in controller/config

I am trying to inject custom factory into controller but i am getting "Unknown provider: ngResourceProvider <- ngResource <- UserService" error message.

When i trying to inject custom filter into config i get below error message `Failed to instantiate module mainApp due to: Error: [$injector:unpr] http://errors.angularjs.org/1.5.0-rc.2/$injector/unpr?p0=... at Error (native) at http://localhost:8080/angulartest/scripts/js/angular.min.js:6:421 at http://localhost:8080/angulartest/scripts/js/angular.min.js:43:7 at d (http://localhost:8080/angulartest/scripts/js/angular.min.js:40:270) at e (http://localhost:8080/angulartest/scripts/js/angular.min.js:41:1) at Object.invoke (http://localhost:8080/angulartest/scripts/js/angular.min.js:41:86) at d (http://localhost:8080/angulartest/scripts/js/angular.min.js:39:234) at http://localhost:8080/angulartest/scripts/js/angular.min.js:39:358 at n (http://localhost:8080/angulartest/scripts/js/angular.min.js:7:366) at g (http://localhost:8080/angulartest/scripts/js/angular.min.js:39:135

I have gone through all sorts of blogs/video i am unable to resolve issue can anybody help me this.

Html

    <html >
<head>
<link rel="stylesheet"
    href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" />
<link rel="stylesheet"
    href="//netdna.bootstrapcdn.com/font-awesome/4.0.0/css/font-awesome.css" />
    <link rel="stylesheet" href="./css/custom.css"/>
<script src="./scripts/js/angular.min.js"></script>
<script src="./scripts/js/angular-local-storage.min.js"></script>
<script src="./scripts/js/angular-route.min.js"></script>
<script src="./scripts/js/jquery.scrollbar.js"></script>
<script src="./scripts/js/script.js"></script>
</head>
<body >
    <header>
        <nav class="navbar navbar-default">
            <div class="container">
                <div class="navbar-header">
                    <a class="navbar-brand" href="/">Angular Routing Example</a>
                </div>

                <ul class="nav navbar-nav navbar-right">
                    <li><a href="#"><i class="fa fa-home"></i> Home</a></li>
                    <li><a href="#about"><i class="fa fa-shield"></i> About</a></li>
                    <li><a href="#contact"><i class="fa fa-comment"></i>
                            Contact</a></li>
                </ul>
            </div>
        </nav>
    </header>

    <!-- MAIN CONTENT AND INJECTED VIEWS -->
    <div id="main" ng-app="myApp" ng-controller="mainController">
        <div ng-view></div>

    </div>
</body>
</html>

java script code for injecting service into controller

var myApp = angular
        .module('myApp', [ 'ngRoute'])

myApp.factory('UserService',['ngResource', function($resource) {

    return $resource('/login', {}, {
        login : {
            method : 'POST',
            params : {
                'action' : 'authenticate'
            },
            headers : {
                'Content-Type' : 'application/x-www-form-urlencoded'
            }
        },
    });
}]);


myApp.config([ '$routeProvider', '$locationProvider', '$httpProvider','$provide',
        function($routeProvider, $locationProvider, $httpProvider,$provide) {
            $routeProvider

            // route for the home page
            .when('/', {
                templateUrl : 'pages/login.html',
                controller : 'mainController'
            })

            // route for the about page
            .when('/dashbaord', {
                templateUrl : 'pages/dashboard.html',
                controller : 'dashbaordController'
            })

            $locationProvider.hashPrefix('!');



        } ]);

myApp.controller('mainController',['UserService',function($scope,UserService) {
    function login() {
        alert("hi");
    }
    $scope.login = login;
}]);

myApp.controller('dashbaordController', function($scope) {
    $scope.message = 'Look! I am an about page.';
});

javascript code for injecting in config , it is same code below i have copied the only changed code

myApp.config([ '$routeProvider', '$locationProvider', '$httpProvider','$provide','UserService',
        function($routeProvider, $locationProvider, $httpProvider,$provide,UserService) {
            $routeProvider

            // route for the home page
            .when('/', {
                templateUrl : 'pages/login.html',
                controller : 'mainController'
            })

            // route for the about page
            .when('/dashbaord', {
                templateUrl : 'pages/dashboard.html',
                controller : 'dashbaordController'
            })

            $locationProvider.hashPrefix('!');



        } ]);

Please include the "angular-resource.js" if you are using ngResource. You can find the cdn in the following link, Angular ngResource api doc


You can only inject provider in .config. Try to transform you UserService into a provider instead of a factory


UserSerivce is a service so you can not inject it like a provider. You did not need to inject service into app.config. you can simply use it by injecting in controller.

链接地址: http://www.djcxy.com/p/13608.html

上一篇: 为什么我应该选择使用AngularJS中的工厂服务?

下一篇: 角度js自定义服务/工厂无法注入控制器/配置