Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using functions from directive controller within link function of same directive

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?

like image 689
Melbourne2991 Avatar asked Dec 31 '13 23:12

Melbourne2991


People also ask

What is the difference between controller and link in directives?

Answer:The link option is just a shortcut to setting up a post-link function. controller: The directive controller can be passed to another directive linking/compiling phase. It can be injected into other directices as a mean to use in inter-directive communication.

What does link function do in a directive?

link function is basically used to manipulate the DOM( Document Object Model ) element using custom directive. link option in custom directive registers DOM listener and also update the DOM.

How do you access the directive variable in a controller?

You just create a myVar variable in your controller and pass it to the directive using my-var attribute. Since you are using two way binding, any changes made to myVar by the directive are available in your controller.

What is the difference between angular js directives and controllers?

A controller is usually used to contain and maintain the logic for your view, which gets bound to your view via $scope. A directive is something that you might use repeatedly and is called in your view directly through the directive name which you can pass in as an attribute.


1 Answers

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

like image 70
Jesus Rodriguez Avatar answered Oct 21 '22 15:10

Jesus Rodriguez