Our team's design pattern for waiting on a directive's template to render is to wrap our DOM manipulation code in a $timeout
(inside the directive's link function), which I know at one time was the normal design pattern. Is that still true, or are there better/safer design patterns to do this?
Example of pattern is in ECMAScript6:
link: ($scope, $element) => {
$timeout(() => {
var domElementFromTemplate = $element.find('myDOMElement');
}
}
It never been a best practise IMO because there is no need to create an asynchronous behavior to an synchronous dom select function. Due to the angular.element documentation it should look like this:
link: ($scope, $element) => {
var domElementFromTemplate = $element.find('myDOMElement');
}
The timeout function should avoid DOM-rendering async behavior but IMO there are a lot more better approaches to deal with this issue:
An other approach is to get the document ready state to ensure you element is available in DOM like:
link: ($scope, $element) => {
angular.element(document).ready(() => {
var domElementFromTemplate = $element.find('myDOMElement');
});
}
An other approach is to create an other directive for those elements, which were rendered inside a directive, e.g. (myDOMElement
) to avoid DOM injection at all.
link: ($scope, $element) => {},
template: "<div some-other-child-directive></div>"
It should be a much better and more neat approach to create a callback function to ensure the element is available in DOM. This could be done by ng-init="someCallback()"
to start up your element functionalities.
Yes, using $timeout
still works fine while a new $timeout
will be added to the to the execution queue and will be executed after the DOM was rendered. A timeout delay value is not needed.
link: ($scope, $element) => {
$timeout(() => {
var domElementFromTemplate = $element.find('myDOMElement');
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With