简体   繁体   English

在将服务注入控制器时获取“未知提供者”

[英]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 - 这个angular-app是C#.NetMVC大项目的一部分,但是编译模式是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? 我如何使用我的服务?

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]);

In your app controller when you call angular. 在您的应用控制器中,当您调用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. module仅传递模块的名称,当您传递数组的第二个参数时,您将重新定义模块并失去依赖模块。

Create module 创建模块

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

Reference a module: 引用模块:

angular.module('name');

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. 如果是这样,很可能您尚未将其中一个文件添加到Bundles.config中。 The error message points that you are using the minified version of Angular. 错误消息指出您正在使用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. 另外,如果将脚本文件添加到包中,请确保您构建了项目,然后对其进行了测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将自定义过滤器注入服务时的“未知提供者” - “Unknown Provider” when injecting custom filter into service Angular在将工厂注入控制器时抛出Unknown provider - Angular is throwing Unknown provider when injecting factory into controller 将指令注入控制器最终导致“ [$ injector:unpr]未知提供程序”错误 - Injecting directive into controller ends up with “[$injector:unpr] Unknown provider” error 在扩展控制器(ES6)中注入模块会导致未知提供程序错误 - Injecting modules in extended controller(ES6) causes Unknown provider error 将自定义服务注入其他模块时,AngularJS中的未知提供程序 - Unknown Provider in AngularJS when Injecting custom service into different module AngularJS:未知提供者:$ scopeProvider &lt; - $ scope error(不在服务中注入$ scope) - AngularJS: Unknown provider: $scopeProvider <- $scope error (not injecting $scope in the service) AngularJS Getting Unknown提供程序错误,将常量注入工厂 - AngularJS Getting Unknown provider error injecting a constant into a factory 向控制器添加服务时的未知提供者 - Unknown Provider when adding service to controller 注入Angular模块:未知提供程序 - Injecting Angular modules: Unknown provider AngularJS,将服务注入控制器 - AngularJS, injecting service into controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM