Directive link order.
On some nested routes the invocation of the directive link functions gets changed:
<div directive1>
<div directive2>
</div>
</div>
The link function invocation for the above should be:
directive1.prelink directive2.prelink directive2.postlink directive1.postlink
However what happens is:
directive1.prelink directive1.postlink directive2.prelink directive2.postlink
This only happens when the template for the view is loaded from the server. For precompiled templates this issue does not exist, nor does it exist for the second loading of a view (when the template is in angular's cache).
I'm not exactly sure what causes this, as it does not happen in all of my views. The particular instance where this is happening currently is a 3-level nested view which is loaded by a ui-sref click. If I open this view directly by going to the route in the browser this problem does not exist.
A lot of directives and nested content depends on the link order so this is important. I have temporarily solved this problem for my code by precompiling the templates.
I suspect what you see is a deferred digest cycle.
Templates loaded using $http are not going to appear until a future digest cycle. We can't know what markup your link function is going to have, and we can't unlink and wait until your templates are loaded.
I think if directive link ordering is a huge issue for you, you're going to have to use your workaround and preload your templates.
I doubt this is the reason since all of the templates defined as templateUrl in directives will be loaded using $http. Not to mention, even preloaded templates are cached in AngularJS and resolved through $http asynchronously. The link order should be preserved, and it is preserved when using AngularJS's built-in routing. The specificity of this issue and the fact that it only happens in some routing configurations makes me think that this is a bug. If this is the expected behaviour for ui-router then it should be documented because this breaks directive behaviour based on how it is specified in AngularJS docs.
Can you make a plunkr?
I believe this is working as expected due to the async nature of templateUrl. Essentially angular doesn't know when the template will be loaded so the compile/prelink/postlink process fundamentally changes. See https://github.com/angular/angular.js/issues/8877#issuecomment-54129804.
https://gist.github.com/eddiemonge/f6a58169c2846731a1eb