I am writing an application on Ruby on Rails using AngularJS for resource management (dynamic loading etc) and JQuery for effects like accordion, fade in-out etc.
When I want to display my data on a page I query the data from the resource using an Angular JS controller and then I display them accordingly using ng-repeat and ng-show. When this is completed I need to setup the JQuery onclick events to add effects (Accordion to be specific). Unfortunately I cannot find an event that will be called when all DOM components are loaded and when all Angular digestion is completed. Meaning the JQuery's onload event is not working and also trying to use a directive like ng-repeat finish event that one produces an error that digest already in progress.
AngularJS is pretty new and I am having difficulties finding good documentation.
Does any one have any ideas? Thanks!
You must wait until ng-repeat has completed before using jQuery to init Accordion.
Here is a directive for doing that ( http://jsfiddle.net/tQw6w/ ); it calls a specified function when true:
.directive('repeatDone', function() {
return function(scope, element, attrs) {
if (scope.$last) { // all are rendered
scope.$eval(attrs.repeatDone);
}
}
})
and the html:
<ul>
<li ng-repeat="feed in feedList" repeat-done="layoutDone()" ng-cloak>
<a href="{{feed}}" title="view at {{feed | hostName}}" data-toggle="tooltip">{{feed | strip_http}}</a>
</li>
</ul>
and the function in the controller:
$scope.layoutDone = function() {
$timeout(function() { $('a[data-toggle="tooltip"]').tooltip(); }, 0); // wait...
}
I have found that $timeout with interval=0 before doing DOM manipulation is required, like initializing tooltips in the fiddle, or your Accordion.
Answered previously here: https://stackoverflow.com/a/16142327/2275421
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