When AngularJS compiles an HTML template it traverses the DOM supplied by the browser and tries to match each element, attribute, comment, and CSS class against its list of registered directives. When it matches a directive, AngularJS calls the directive's compile function, which returns a linking function. AngularJS collects all of these linking functions.
The compilation stage is done before the scope has been prepared, and no scope data is available in the compile function.
Once all the directives have been compiled, AngularJS creates the scope and links each directive to the scope by calling each of the linking functions.
At the linking stage, the scope is being attached to the directive, ...