Hi I am facing an issue with my nested directives. The parent directive, along with its child directives that can be transcluded, is causing a problem. When attempting to find some DOM elements in the parent link function, it does not return an array without setting a timeout. It seems like the rendering/transclusion of the child is not happening quickly enough. Is there a solution to this problem without using a timeout and then calling to find the child elements?
var app = angular.module('plunker', [])
.run(function($templateCache){
$templateCache.put('innerPane.html', "<div class='inner-pane' ng-transclude></div>");
$templateCache.put('slidePaneSelector.html', "<div class='slide-pane'><inner-pane><h2>First Inner Pane</h2></inner-pane><inner-pane><h2>Second Inner Pane</h2></inner-pane></div>");
})
.directive('innerPane', function(){
return {
restrict: 'EA',
transclude: true,
replace: true,
templateUrl: 'innerPane.html',
scope:{},
link: function(scope,element,attr){
}
}
})
.directive('slidePaneSelector', ['$timeout',function($timeout){
return {
restrict: 'EA',
transclude: true,
replace: true,
templateUrl: 'slidePaneSelector.html',
scope:{},
link: function(scope,element,attr){
var firstPaneElement = angular.element(element.find('div')[0]);
var secondPaneElement = angular.element(element.find('div')[1]);
console.log(element.find('div'));
$timeout(function(){
console.log(element.find('div'));
},100);
}
}
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css" />
<script data-require="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6e0f00091b020f1c40041d2e5f405c4016">[email protected]</a>" src="https://code.angularjs.org/1.2.28/angular.js" data-semver="1.2.28"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
<p>Hello {{name}}!</p>
<slide-pane-selector></slide-pane-selector>
</body>
</html>
Plunker: http://plnkr.co/edit/sS5cSMboSV2mjlRxsgO8?p=preview