【问题标题】:Error: [$injector:unpr] Unknown provider错误:[$injector:unpr] 未知提供者
【发布时间】:2014-08-05 22:13:15
【问题描述】:

我已经使用名为 uIdentity 的 Angular 设置了一个服务。但是,当我尝试这样称呼它时:

app.controller('foo', function(uIdentity){});

我收到此错误消息:

错误:[$injector:unpr] 未知提供者:uIdentityProvider

请问我该如何解决?

【问题讨论】:

    标签: angularjs


    【解决方案1】:

    试试这个

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

    如果您得到错误,尽管如此,您的 uIdentity 本身也有问题。

    一般来说,您可能希望养成始终使用该语法的习惯。它还可以使您的代码免受丑化/缩小/混淆的影响。

    确保定义了依赖项

    app.service('uIdentity', [function(){
    
    }]);
    

    【讨论】:

    • 在答案中添加了一个带有 $scope 的示例,因为我无法在评论中格式化代码。顺序很重要。
    • 请发帖或用其他部分更新问题
    【解决方案2】:

    您必须确保您的提供程序与您的应用程序位于相同的 angular.module 中,或者您的应用程序取决于您的提供程序所在的模块。

    如果您的 uIdentity-provider 是名为 myIdentityModule 的模块的一部分,例如您的应用程序需要 myIdentityModule 作为依赖项。

    你的代码可能是这样的

    angular.module('app', ['myIdentityModule'])
    

    【讨论】:

      猜你喜欢
      • 2017-04-25
      • 1970-01-01
      • 2015-01-25
      • 2016-03-26
      • 2014-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多