I have one auto-carousel
directive which iterates through the linked element's children.
The children however are not yet loaded in the DOM, because their ng-if
s expressions have not been parsed yet.
How can I make sure the parent directive knows there have been changes to it's DOM tree?
<ul class="unstyled" auto-carousel>
<li class="slide" ng-if="name">{{name}}</li>
...
<li class="slide" ng-if="email">{{email}}</li>
</ul>
I could use $timeout
but that feels unreliable. I could also use ng-show
instead of ng-if
but that does not answer the question and not what I need.
If you need to watch for any changes deeper in the element's dom, MutationObserver is the way to go :
You could try to compile the directive contents first inside your link function. For example:
I created a directive module for this angular-dom-events
In your case you could
Currently only supports
dom-on-create
anddom-on-destroy
, but has better performance then the accepted answer because it will only fire once for each dom event, rather than repeatedly check the $watch callback.So here's what I ended up doing:
I discovered you could pass a function to
$scope.$watch
. From there, it's pretty straightforward to return the value of the expression you want to watch for changes. It will work exactly like passing a key string for a property on the scope.I am watching
childNodes
, notchildren
, because thechildNodes
list holds elements as well as text nodes and comments. This is priceless because Angular uses comment placeholders for directives likeng-repeat
,ng-if
,ng-switch
andng-include
which perform transclusion and alter the DOM, whilechildren
only holds elements.Although I don't think it is with angular's recommendations, you could use ng-init which fires upon the initialization of the element: