Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using "require" in Directive to require a parent Controller

Tags:

angularjs

I try to "require" a parent controller (not directive) but AngularJS returns an exception. The code is like this:

JS

app.controller("myController", function ($scole) {
    ...
});

app.directive("myDirective", function ($q) {
    return {
        require: "^myController",
        template: "",
        link: function (scope, element, attrs, myCtrl) {
            ...
        }
    };
});

HTML

<div ng-controller="myController as myCtrl">
    ...
        <div my-directive>...</div>
    ...
</div>

Error

Error: [$compile:ctreq] Controller 'myController', required by directive 'myDirective', can't be found!

Why?
Maybe, require property must be reference to a controller of directive?

Thanks

like image 731
riofly Avatar asked Jun 05 '15 18:06

riofly


People also ask

How to require the outer controller in a directive?

This is not very conventional what you are trying to do, but if you really want to require outer controller in your directive you can require ngController: app.directive ("myDirective", function ($q) { return { require: "^ngController", template: "", link: function (scope, element, attrs, myCtrl) { // ... console.log (myCtrl); } }; });

What is the use of require property of controller?

You are correct that the require property is for referencing the controllers of other directives. I had suspected, but could not find anything in the official documentation to give me this certainty. Thanks Alex. Require is of using other directives controllers in another directive , please refer the below example

How do I share data between a controller and a directive?

For example, if you needed your directive to work with the ngModel directive then you'd use this feature to require ngModel 's controller so you can call stuff on it like a little API. If you need to share data between directives and controllers, look into services and the various isolate scope binding syntaxes.

How do I communicate between directives?

Communication between Directives can be done in various ways. When dealing with Directives that have a hierarchical relationship we can use Directive Controllers to talk between them.


1 Answers

Notation require: "^myController" means that your directive will try to access another directive called myController and defined on some of the ancestor tags as my-controller attribute or <my-controller> tag. In your case you don't have such directive, hence the exception.

This is not very conventional what you are trying to do, but if you really want to require outer controller in your directive you can require ngController:

app.directive("myDirective", function($q) {
    return {
        require: "^ngController",
        template: "",
        link: function(scope, element, attrs, myCtrl) {
            // ...
            console.log(myCtrl);
        }
    };
});

However, this is not very good idea. I can't imagine why you might need it like this. I would recommend to look into scope configuration properties and how you can pass executable function references into your directive from outer controller.

<div my-directive some-callback="test()"></div>

and in directive define scope:

scope: {
    someCallback: '&'
}

where in controller you would have $scope.test = function() {};. Then you would not need to require controller explicitly in directive.

like image 124
dfsq Avatar answered Sep 22 '22 11:09

dfsq