look_right
look_right

Reputation: 33

Getting "Unknown Provider" while injecting service into controller

I have my App:

'use strict';
angular.module('ProjectTracker', [
 'App.controllers',
 'App.services'
])

My Service:

angular.module("App.services", [])
.factory('ProjectManager', function () {
    return {
        getAll: function () {
            return [{ID:'123123', Name:'2313'}];
        }
    }
});

My controllers:

var appCtrl = angular.module('App.controllers', ['App.services']);
var ProjectsListController = ['$scope', 'ProjectManager', function ($scope, ProjectManager) {
    $scope.projectList = ProjectManager.getAll();
}];
appCtrl.controller('ProjectsListController', ProjectsListController);

But when I start, it throws me this:

Error: [$injector:unpr] http://errors.angularjs.org/1.2.16/$injector/unpr?p0=ProjectManagerProvider%20%3C-%20ProjectManager t/<@http://localhost:14967/Areas/ProjectTrack/Scripts/angular    .min.js:6 ac/m.$injector<@http://localhost:14967/Areas/ProjectTrack/Scripts/angular.min.js:35 c@http://    localhost:14967/Areas/ProjectTrack/Scripts/angular.min.js:34 ac/p.$injector<@http://localhost:14967/Areas/    ProjectTrack/Scripts/angular.min.js:36 c@http://localhost:14967/Areas/ProjectTrack/Scripts/angular.min.js:34     d@http://localhost:14967/Areas/ProjectTrack/Scripts/angular.min.js:34 ac/g/<.instantiate@http://localhost:14967/    Areas/ProjectTrack/Scripts/angular.min.js:34 Od/this.$get</<@http://localhost:14967/Areas/ProjectTrack/Scripts/    angular.min.js:66 cc/this.$get</ia/J/<@http://localhost:14967/Areas/ProjectTrack/Scripts/angular.min.js:53 q@http://    localhost:14967/Areas/ProjectTrack/Scripts/angular.min.js:7 J@http://localhost:14967/Areas/ProjectTrack/Scripts/    angular.min.js:52 f@http://localhost:14967/Areas/ProjectTrack/Scripts/angular.min.js:46 f@http://localhost:14967/    Areas/ProjectTrack/Scripts/angular.min.js:46 cc/this.$get</x/<@http://localhost:14967/Areas/ProjectTrack/Scripts/    angular.min.js:46 $b/c/</<@http://localhost:14967/Areas/ProjectTrack/Scripts/angular.min.js:18 Yd/this.$get</h.    prototype.$eval@http://localhost:14967/Areas/ProjectTrack/Scripts/angular.min.js:108 Yd/this.$get</h.    prototype.$apply@http://localhost:14967/Areas/ProjectTrack/Scripts/angular.min.js:109 $b/c/<@http://localhost:14967/    Areas/ProjectTrack/Scripts/angular.min.js:18 d@http://localhost:14967/Areas/ProjectTrack/Scripts/angular.min.js:34     $b/c@http://localhost:14967/Areas/ProjectTrack/Scripts/angular.min.js:18 $b@http://localhost:14967/Areas/    ProjectTrack/Scripts/angular.min.js:18 Wc@http://localhost:14967/Areas/ProjectTrack/Scripts/angular.min.js:17     @http://localhost:14967/Areas/ProjectTrack/Scripts/angular.min.js:209 jQuery.Callbacks/fire@http://localhost:14967/    Scripts/jquery-2.0.3.js:2913 jQuery.Callbacks/self.fireWith@http://localhost:14967/Scripts/jquery-2.0.3.js:3025 .    ready@http://localhost:14967/Scripts/jquery-2.0.3.js:398 completed@http://localhost:14967/Scripts/jquery-2.0.3.js:93

I've tried a plenty of methods to inject my service, but nothing works. This angular-app is a part of the big C# .NetMVC project, but compliation mode is debug -

<compilation debug="true" targetFramework="4.5" />

So, it shouldn't rename my variables (minification should be turned off). How could I use my service?

Upvotes: 1

Views: 4147

Answers (3)

Brocco
Brocco

Reputation: 64893

In your app controller when you call angular. module only pass in the name of the module, when you pass in the array s the 2nd parameter you are redefining the module and losing a dependency module.

Create module

angular.module('name', []);

Reference a module:

angular.module('name');

Upvotes: 2

mithun_daa
mithun_daa

Reputation: 4384

Are these in separate files? the module setup, the service and the controller? if so, most likely you have not added one of the files to your Bundles.config. The error message points that you are using the minified version of Angular. I would suggest you change that to the debug version so you get to see a more meaningful message. I have plunk with all of these in the same file and it works just fine. That is what leads me to believe that you are missing a reference. Also, if you add a script file to your bundles, make sure you build the project and then test it.

Upvotes: 1

Tim
Tim

Reputation: 477

I think your declaring the dependencies at the wrong place..

I do it like this:

var appCtrl = angular.module('App.controllers', ['App.services']);
var ProjectsListController =  function ($scope, ProjectManager) {
$scope.projectList = ProjectManager.getAll();
};
appCtrl.controller('ProjectsListController', ['$scope', 'ProjectManager', ProjectsListController]);

Upvotes: 2

Related Questions