Search code examples
javascriptangularjsangular-directive

AngularJS - accessing the directive object inside the controller


Let's say that I have define a simple directive:

app.directive('someDirective', [function() {
    return {
        restrict: 'E',
        link: function() {

        },
        controller: [function() {
           // Access directive object here...
        }]
    }
}]);

Can I access the generated someDirective object inside someDirective's controller function? I know that the this property reference the directive object inside the compile and template functions, but i don't know how to access the directive object inside the controller function. Any tricks?

Thanks.


Solution

  • Sure you can, not sure why you'd want too though...

    app.directive('someDirective', [function() {
        var directiveObject = {
            restrict: 'E',
            link: function() {
    
            },
            controller: [function() {
               // Access directive object here...
               directiveObject.whatever
            }]
        }
    
        return  directiveObject;
    }]);