One thing that may confuse their description is that they are trying to discuss the idea of ββa directive in <ng-repeat> , rather than discussing <ng-repeat> .
The idea is that even if you have several instances of a particular directive (for example, because they are within the <ng-repeat> ), the compilation function is executed once and only once for the life of your application. Thus, the advantage of using code here is that it runs only once. And this is also a potential problem. The only thing that should go into the compilation function is things that are common to all instances of this directive.
The link function, on the other hand, is executed once for each instance of this directive (again, for example, within <ng-repeat> ).
So, you can think of the compilation function as setting up a template of what should be a directive of this type, while the link function sets up the actual instance of this directive. This is why the link function gets the $ scope passed to it, and the compilation does not work, and why the link function is much more often used.
For an excellent discussion of exactly this by one of the authors of Angular, check: http://www.youtube.com/watch?v=WqmeI5fZcho&list=TLwY_LmqLDXW_3QKhTNm1zKa9j40TvPO2O (13:42 is where Misko is the link address and compilation functions)
Kayak dave
source share