The ability to reply to discussions is limited to PRO members. Want to join in the discussion? Click here to subscribe now.

Directives Talking to Controllers

Directives Talking to Controllers

4:43
Often in AngularJS we want to share information between controllers and directives. One way to do this is by passing the scope, but this makes the directive reliant on the scope having the methods you want to run. This lesson shows you a cleaner way that you can pass methods to the directive using an attribute and decouple controllers from directives. This in turn makes your directives more generic/reusable.
Watch this lesson now
Avatar
egghead.io

Often in AngularJS we want to share information between controllers and directives. One way to do this is by passing the scope, but this makes the directive reliant on the scope having the methods you want to run. This lesson shows you a cleaner way that you can pass methods to the directive using an attribute and decouple controllers from directives. This in turn makes your directives more generic/reusable.

Avatar
marcek

What if you want to keep the directive isolated from the controller's scope ? Wouldn't be better to pass the controller's method reference to the directive by "&" ?

app.directive("enter", function() {

    var linkFn = function(scope, element, attrs) {
        element.bind("mouseenter", function() {
                scope.enter()
        })
    }

    return {
        scope: {
            enter: '&'
        },
        link: linkFn
    }
})
In reply to egghead.io
HEY, QUICK QUESTION!
Joel's Head
Why are we asking?