Chris Cooper
Chris Cooper

Reputation: 899

Requiring controller from ng-controller parent - Controller required by directive can't be found

I have the following AngularJS fragment:

<div ng-controller="FooController as fooCtrl">
     <bar-directive></bar-directive>
</div>

And the directive for bar-directive looks something like:

angular.module("Foo").directive('barDirective', function() {
    return {
        restrict: 'E',
        require: ['^FooController'],
        link: function (scope, element, attrs, controllers) {
            console.log(controllers);
        }
    };
});

I then get an error saying "Controller 'FooController', required by directive 'barDirective', can't be found". Can controllers not be inherited this way? Do I need to create a wrapper directive of my own?

Upvotes: 0

Views: 131

Answers (1)

melsaqqa
melsaqqa

Reputation: 194

require is used to require another directive and inject its controller as the fourth argument to the linking function, you need to require ngController directive

require: ['^ngController'],

Upvotes: 1

Related Questions