One thing that can be confusing about their description is they're trying to discuss the idea of a directive within an <ng-repeat>
as opposed to discussing <ng-repeat>
itself.
The idea is that even if you have multiple instantiations of a particular directive (for instance because they are within an <ng-repeat>
) the compile function is executed once and only once for the lifetime of your app. Thus, the performance benefit of putting code in here is that it only gets run only once. And that's also the potential problem. The only things that should go in a compile function are things that are common to all the instantiations of that directive.
The link function, on the other hand, is executed once for each instantiation of that directive (again, for example, within an <ng-repeat>
).
So you can think of the compile function as setting up the template of what a directive of this type should be, while the link function sets up an actual instance of that directive. This is why the link function gets a $scope passed in to it and the compile doesn't and why the link function is the much more commonly used one.
For a great discussion of exactly this by one of the authors of Angular, check out: http://www.youtube.com/watch?v=WqmeI5fZcho&list=TLwY_LmqLDXW_3QKhTNm1zKa9j40TvPO2O (13:42 is where Misko address link vs compile functions)