Angularjs 错误:[$injector:unpr]未知提供程序
我已经使用Angular设置了一个名为uIdentity的服务。但是,当我试着这样称呼它时:Angularjs 错误:[$injector:unpr]未知提供程序,angularjs,Angularjs,我已经使用Angular设置了一个名为uIdentity的服务。但是,当我试着这样称呼它时: app.controller('foo', function(uIdentity){}); 我收到以下错误消息: 错误:[$injector:unpr]未知提供程序:uIdentityProvider请尝试此操作 app.controller('foo', ['uIdentity',function(uIdentity){ }]); 或者有更多的项目 app.controller('foo', [
app.controller('foo', function(uIdentity){});
我收到以下错误消息:
错误:[$injector:unpr]未知提供程序:uIdentityProvider请尝试此操作
app.controller('foo', ['uIdentity',function(uIdentity){
}]);
或者有更多的项目
app.controller('foo', ['$scope','uIdentity',function($scope, uIdentity){
}]);
基本上,您是在定义对象时定义对象的名称,它们将按顺序映射。另一个例子:-
app.controller('foo', ['$scope','uIdentity',function($x, y){
//$x is $scope and y is uIdentity
}]);
如果你尽管有错误,但还是犯了错误,那么你的身份本身就有问题
一般来说,您可能希望养成始终使用该语法的习惯。它还使您的代码能够抵御丑陋/缩小/混淆
确保定义了依赖项
app.service('uIdentity', [function(){
}]);
您必须确保提供程序与应用程序位于同一个angular.module中,或者应用程序取决于提供程序所在的模块 如果您的
UIIdentity
-提供程序是名为myIdentityModule
的模块的一部分,例如,您的应用程序需要将myIdentityModule
作为依赖项
您的代码可能类似于
angular.module('app', ['myIdentityModule'])
由于我无法格式化注释中的代码,因此在回答中又添加了一个带有$scope的示例。顺序很重要。请发布一个plunker或用其他部分更新问题