I am using ui-router in angular js to nest views. A directive in one of my parent views requires to watch if a sub-view is reloaded. Can anyone tell me how would I watch if a sub-view inside a view is reloaded?
Here, the $watch statement only watches the parent view load. What I need to do is to add the event listener for ion-content element everytime the sub view (bodyContent) is changed or reloaded.
I used $stateChangeSuccess to check if the state has changed. That worked fine. I have been able to detect change in states. But the $stateChangeSuccess event is fired before the DOM of the view is rendered. That is why the event listener binding does not occur. Is there a way to wait until the DOM of the view is rendered before binding the event listener?
Just to post that I found a different solution. I didnt think using timeout was really a “classy” solution. So i did a little digging and went through all the variables that $stateChangeSuccess was returning. While digging through the event variable I saw the targetScope variable. I was then able to watch the $viewContentLoaded variable on the targetscope to see if the view has been loaded. the code looks something like this
scope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams){
event.targetScope.$watch('$viewContentLoaded', function(){
var contentBody = element.parent().find("ion-content");
var contentHead = element;
contentHead.on("touchStart click", function(event){
//Do something
});
contentBody.on("touchStart click", function(event){
Do Something
});
});
})
I hope this is gonna be useful to others as well.
@bengtler Thanks for helping and pointing me to the right direction.