【问题标题】:AngularJS: Directive can not find dependency injectedAngularJS:指令找不到注入的依赖项
【发布时间】:2013-05-24 05:50:41
【问题描述】:

这是我的小伙伴 - http://plnkr.co/edit/uqSB1gIz6XEmJfC8zHNb?p=preview

我正在尝试注入Angular Strap

index.html 包含正确的依赖项和正确的顺序

<script data-require="jquery@*" data-semver="2.0.1" src="http://code.jquery.com/jquery-2.0.1.min.js"></script>
<script data-require="angular.js@1.1.5" data-semver="1.1.5" src="http://code.angularjs.org/1.1.5/angular.min.js"></script>
<script data-require="bootstrap@2.3.2" data-semver="2.3.2" src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
<script data-require="angular-strap@0.6.6" data-semver="0.6.6" src="//cdnjs.cloudflare.com/ajax/libs/angular-strap/0.6.6/angular-strap.min.js"></script>

我的控制台说

Error: Unknown provider: $strap.directivesProvider <- $strap.directives <- notificationDirective
@http://code.angularjs.org/1.1.5/angular.min.js:29
c@http://code.angularjs.org/1.1.5/angular.min.js:27
@http://code.angularjs.org/1.1.5/angular.min.js:30
c@http://code.angularjs.org/1.1.5/angular.min.js:27
d@http://code.angularjs.org/1.1.5/angular.min.js:27
@http://code.angularjs.org/1.1.5/angular.min.js:37
forEach@[native code]

【问题讨论】:

    标签: javascript html angularjs angularjs-directive angularjs-module


    【解决方案1】:

    AngularStrap 的模块依赖列在错误的位置。 '$strap-directives' 是模块依赖,而不是服务依赖,所以需要在应用启动时列出:

    var app = angular.module('myApp', ['$strap.directives']);
    

    它还需要在您的指令中作为服务 DI 删除:

    app.directive('notification', function(){
        return {
            restrict: 'E',
            replace: true,
            scope: {
                ngModel: '='
            },
            template: '<div>Test</div>'
        }
    });
    

    这也是他们在AngularStrap site 上配置 Plunker 的方式。

    【讨论】:

      【解决方案2】:

      $strap.directives 是一个模块。您需要将其定义为您自己模块的依赖项:

      var app = angular.module('myApp', ['$strap.directives']);
      

      您无需将其注入指令中。因此,您的指令只会看起来像:

      app.directive('notification', function(){
        return {
          restrict: 'E',
          replace: true,
          scope: {
            ngModel: '='
          },
          template: '<div>Test</div>'
        }
      });
      

      【讨论】:

      • 这将引发错误,因为该指令尝试注入 strap 失败
      • 啊,是的!编辑了我的帖子。非常感谢!
      猜你喜欢
      • 2014-12-04
      • 1970-01-01
      • 2020-05-01
      • 2015-08-28
      • 1970-01-01
      • 1970-01-01
      • 2014-11-21
      • 1970-01-01
      • 2016-03-17
      相关资源
      最近更新 更多