1

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('!');



        } ]);

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

tried no use <link rel="stylesheet" href="cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0/…
please try <script src="ajax.googleapis.com/ajax/libs/angularjs/1.4.5/…> because the tag you used is to include stylesheets/css and not for javascript files.
1

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

1 Comment

but i want to use factory cant i inject factory?
1

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.

3 Comments

i am unable to inject into controlller too
do you remove injection from app.config? If yes, then whats the error?
If you can make a plnkr, it would be easier to help.
0

There are couple of things went wrong.

  1. You are passing factory service to your module configuration like provider which is wrong.
  2. ngResource DI must be used to your module and not to the factory.
  3. $resource DI must be used to factory

Here is the plunker with right code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.