boyomarinov
boyomarinov

Reputation: 615

How to access both child and parent controller in directive?

We have two directives, called parent and child. Both of them have controllers defined, which hold some functionality. For child directive we can:

So the question is: how can we reference both child and parent controllers in child's link function? Here's a plunker with the example: http://plnkr.co/edit/WzU6iJgf2zLLApFHeiEG?p=preview

Upvotes: 3

Views: 81

Answers (1)

Michael Kang
Michael Kang

Reputation: 52847

You can pass an array to the 'require' property of your directive definition, which includes both your child directive, and your parent directive. The 4th parameter to your link function will be an array:

app.directive('childDirective', function() {
    require: ['childDirective', '^parentDirective'],
    link: function(scope, element, attr, ctrls) {
        var childCtrl = ctrls[0];
        var parentCtrl = ctrls[1];
        ...
    }
});

Upvotes: 5

Related Questions