Search code examples
javascripthtmlangularjsangularjs-directiveangularjs-module

AngularJS: Directive can not find dependency injected


Here is my plunker - http://plnkr.co/edit/uqSB1gIz6XEmJfC8zHNb?p=preview

I am trying to inject Angular Strap

The index.html is including the right dependencies and in correct order

<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>

My console says

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]

Solution

  • The module dependency for AngularStrap is listed in the wrong place. '$strap-directives' is a module dependency, not a service dependency, so it needs to be listed during the app bootstrap like:

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

    It also needs to be removed as a service DI in your directive:

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

    This is the way they configure the Plunkers on the AngularStrap site as well.