Reputation: 71
How I can get element with class .second
from second
directive in link
function of first
directive?
http://plnkr.co/edit/PiCyZzgvdwAuOyNKOi9E?p=preview
P.S it works with template
in link
functions, but I need templateUrl
.
Upvotes: 1
Views: 41
Reputation: 19193
Since you are using templateURL, the HTML need to be "downloaded" and then it is an asynchronous event which takes some time.
You could work around that with a timeout, but that wouldn't be clean as you don't know how long to wait.
One option, if <second>
is supposed to always be inside <first>
, is to run a callback from it once it is ready, from its own link function:
{
restrict: "E",
templateUrl:'second.html',
link: function(scope) {
scope.onSecondReady();
}
}
Check this plunkr for the full code:
http://plnkr.co/edit/NLdWaL8zRYkroGC7ZkYL?p=preview
Now if <second>
is supposed to be re-usable in different context, and not always within the first one, then you need to use events. Once second is ready, you use scope.$emit('second-is-ready')
and from the first one you listen to it scope.$on('second-is-ready')
Upvotes: 2