Parent directive goes through compile, pre, and post before child directive with templateUrl
I think there is a bug with the ordering of compile, pre, and post linking functions in parent directives when a child directive uses templateUrl to get the template.
Here are some simple directived, not using the template or templateUrl properties:
http://plnkr.co/edit/gEk3GjuASxrV1L6Kp6Dq?p=preview
Everything happens in the order I expect.
Next we use the template property for the logCompile2 directive:
http://plnkr.co/edit/EOYgf5z82mesixcb1LXU?p=preview
Again everything works as expected.
Finally we use the templateUrl property for the logCompile2 directive:
http://plnkr.co/edit/A55sw6YRCuhvWUi6zQ4Q?p=preview
This is where things go wrong as the parent directive is running through all the steps before any of the steps of the child directives are linked.
Some else played with this that last plunker and added transclusion with weird results too:
http://plnkr.co/edit/0jjwTk8VGoqjSF9se1KZ?p=preview
The documentation states:
Post-linking function
Executed after the child elements are linked. It is safe to do DOM transformation in the
post-linking function.
Either I am miss understanding the documentation or this is a bug as I would except the child directive to compile and run pre/post linking before the parent's post linking would run (as the non-templateUrl example do).
Is this a bug or am I miss understanding something?
I should mention this is an issue with 1.2.23 and 1.3.0-rc.0
We have a large SPA and use many directives in the parent/child scenarios with templateUrl, we have just upgraded from 1.12.15 to 1.5.8 mainly to address memory leak issues with the earlier version (and its interaction with jquery). Until now i had not considered (or in truth been aware) that templateUrl was asynchronous but under the new angular we are seeing some bad transient rendering of child templates particularly under IE10 where entire templates are being displayed without consideration of ng-show/hide conditional flags e.g. we get a 'load error' chunk of html displayed alongside a chunk of user input html, something that was intended to be mutually exclusive. Its a short lived flicker but very noticeable.
I am seeing improvement if i use something like 'template' : '<div ng-include="directiveHeldUrlPath"></div>' in the directives and hope this proves to be a suitable approach.
As it looks like templateUrl has always been asynchronous it is worth noting that the impact under angular 1.5.8 is greater than 1.2.15, just something to consider if you are planning a similar upgrade.
Faced similar issue with unobvious order of linking functions when using async templateUrls,
with respect to scope manipulation.
The case was:
-
Child
.componentcontroller(1.5) depends on -> Parent.directivescope. Parent.directive'spostLinkmanipulated scope (causing it to beundefinedin the child's bindings during linking stage). The solution that worked for us was: -
to move scope setup out of
.directivepostLinkfn -> into.directivecontrollerfn (which gets invoked before bothpostLink's) and manipulating with injected$scopethere

Notice "Parent directive controller" log being topmost in both steps.
P.S> I guess, one might also want to add $onChanges for more reliability in the child component, but in our case it still worked well without it P.P.S>If anyone knows a cleaner solution, please share :)
In reply to: https://github.com/angular/angular.js/issues/8877#issuecomment-103898850
There is no need to use watchers. VanillaJS setInterval works perfect. But thanks for pointing to good solution.
My code:
link: function(scope, element, attrs, ctrls) {
var time = Date.now();
//run the check every 0.5 seconds
var interval = setInterval(function() {
//once the content we were waiting for is loaded stop this checking and run the necessary code
if(element.find('.something-to-be-loaded').length) {
run();
clearInterval(interval);
return;
}
//prevent forever checking, fail after 30 seconds
var now = Date.now();
if(now - time > 30000) {
console.error('failed...', element);
clearInterval(interval);
}
}, 500);
function run() {
///
}
}
Hi All
I wanted to execute the function when the child directive DOM has been loaded & bindingVariable has also been updated in DOM.
<div class='childDOM'>{{bindingVariable}}</div>
I used both the approaches i.e using $watch & using $timeout function .
In case of $watch we can come to know weather DOM is being loaded i.e
<div class='childDOM'></div> is loaded , but till then the {{bindingVariable}} has not been updated in DOM.
In case of $timeout we are not sure that DOM would be loaded with in the specified timeout or not ?
@ryanzec @petebacondarwin Is there any standard or more better approach in which we are sure that both DOM & bindingVariable is also updated ?
@ChaitanyaBabar - I am afraid that is not something that can be done reliably in AngularJS. As mentioned before, executing code based on the DOM changing is against the design of AngularJS where the application logic should be agnostic to the view.
Hi All,
I have nested child-directives using the templateUrls , & I have a top-level directive in which I want to traverse the DOM after all the nested child-directives are complied .
<div top-level-directive><child-1></child-1><child-2></child-2></div>
I wrote the domTraversalFn() inside the link of top-level directive , but still not sure that each time all child DOM is traversed . I also tried using highest priority top-level-directive so that post-link is executed at last (as per docs) , but no luck :(
@petebacondarwin So why does the priority does not work as expected ? Is it due to the child-directives that are using templateURl's? I know that code based on the DOM is against the desgin of AngularJS , but can i find any approach where i am sure that all my child directives are loaded in DOM ?