0

The title may be a bit misleading, but i could not think of a better description.

I wrote a directive which includes a ng-repeat:

app.directive('appDirective',function($purr){
    var template = '' +
        '<div ng-repeat="elements in queue">' +            
        '</div>';

    return{
        template: template
    }
});

If i'm correct, i can choose between two ways of providing queue to my directive

1: via linking function

    return{
        restrict: 'A',
        template: template,
        link: function(scope){
                scope.queue =[];
        }
    }

2: via controller

    return{
        restrict: 'A',
        template: template,
        controller: directiveCtrl
    }

app.controller('directiveCtrl',function($scope){
    $scope.queue = [];
});

Which way should i choose, and why?

user2422960
  • 1,476
  • 6
  • 16
  • 28

1 Answers1

1

There is little difference between a directive's link function and controller function. In general, you can put methods, $watches, etc. into either. The controller will run first, which sometimes matters. You may want to put scope-manipulation functions inside the controller just for consistency with the rest of the framework.

This fiddle logs when the controller and link functions run with two nested directives.

See also Difference between the 'controller', 'link' and 'compile' functions when defining a directive

.
Community
  • 1
  • 1
Mark Rajcok
  • 362,217
  • 114
  • 495
  • 492
  • Thanks for the clarification. Personally, I'd say that it feels more correct to apply a controller. On the other hand, i would have thought that the second way, is the more efficent one. – user2422960 Jul 26 '13 at 22:16