Directive allocation inside a parent template:
<div badge></div>
Directive template templates/badge.html
:
Notice allocation of the dynamic id, using directive $id
.
<div>
<span id="id{{ ::$id }}_name">Nik Sumeiko, Frontend engineer</span>
</div>
Directive:
angular.module('app').directive('badge', () => ({
restrict: "A",
replace: true,
templateUrl: "templates/badge.html",
link: (scope, element, attributes) => {
// Tries to query nested DOM element by a dynamic selector.
const name = element.find(`#id${scope.$id}_name`);
console.log(name.length, element.html());
}
}));
Based on the console output it's clearly visible that directive template haven't compiled its dynamic values yet:
0 "
<div>
<span id="id{{ ::$id }}_name">Nik Sumeiko, Frontend engineer</span>
</div>
"
How then it is possible to query nested elements by a dynamic selector? Is there any other directive methods that are dispatched after Angular rendered dynamic values of the template?
Please don't suggest to use $timeout
injected function to delay template rendering inside link
method, since I don't think it is the right way…