36

Perhaps I have a fundamental misunderstanding of how directive controllers work, from what I understand they are used as a sort of API to be exposed to other directives & controllers. I am trying to get the controller and link function to communicate internally.

For example I would like to be able to set a variable via the controller function and then use it in the link function:

var app = angular.module('test-app', []);

app.directive('coolDirective', function () {
    return {
        controller: function () {
            this.sayHi = function($scope, $element, $attrs) {
                $scope.myVar = "yo"
            }
        },
        link: function(scope, el, attrs) {
            console.log(scope.myVar);
        }   
    }
});

How can I access myVar or sayHi within the link function? Or have I just missed the point completely?

2 Answers 2

61

Both controller's $scope (defined in the controller, not in the sayHi function) and link scope are the same. Setting something in the controller will be usable from the link or viceversa.

The problem you have is that sayHi is a function that is never fired so myVar is never set.

Since sayHi is not in the scope, you need a reference to the controller and to do it, you can add a fourth parameter like this:

link: function(scope, element, attr, ctrl) {}

Then you could do a ctrl.sayHi() (But again, those params of sayHi belongs to the controller function.)

If you ever need to require another controller and still wanting to use its own directive, then you will need to require it too. So if this coolDirective needs to access to the controller of notCoolAtAll you could do:

require: ['coolDirective', 'notCoolAtAll']

That will do the trick. The link function will receive then an array of controllers as the fourth param and in this case the first element will be coolDirective ctrl and the second one the notCoolAtAll one.

Here is a little example: http://plnkr.co/edit/JXahWE43H3biouygmnOX?p=preview

Sign up to request clarification or add additional context in comments.

4 Comments

perfect thanks heaps, makes much more sense to me now!
hello ... if I add console.log( scope.name ) ; within your link function, it returns undefined ... how can I access MainCtrl's .name property ... thanks ... (see forked plunker: plnkr.co/edit/STVVT5Vj9tkV9t0LFxKu?p=preview)
Perfect answer. Not able to figure out how to do until I see your answer :)
Is there any way to access the "element" variable from link function in the controller??
0

Rewriting your code above, it would look something like this:

var app = angular.module('test-app', []);

app.directive('coolDirective', function() {
    return {
        controller: function($scope) {
            // bind myVar property to scope
            $scope.myVar = 'yo';
            // bind sayHi method to scope
            $scope.sayHi = sayHi;
            // abstracting out the sayHi function
            function sayHi() {
                console.log($scope.myVar);
            }
        },
        link: function(scope, el, attrs) {
            // execute the sayHi function from link
            scope.sayHi(); // "yo" in console
        }
    };
});

Good Luck.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.