Skip to content

Instantly share code, notes, and snippets.

@bmakarand2009
Created August 29, 2014 23:58
Show Gist options
  • Save bmakarand2009/37c0ef8ac249b87f1ae7 to your computer and use it in GitHub Desktop.
Save bmakarand2009/37c0ef8ac249b87f1ae7 to your computer and use it in GitHub Desktop.
How to use $last to run JQuery script after angular ngRepeat has loaded the table
//Step1 -- ngRepeat directive to go through all the rows in the table
//emp.html - we see a snipped code which is conrolled by the empController
<div ng-controller="empController">
<table id="myTable" class="emp-table">
<thead>
<tr>
<th>Name</th>
<th>Address</th>
</tr>
</thead>
<tbody ng-repeat="emp in employees">
<td class="empRow"> {{emp.name}} </td>
<td class="empRow"> {{emp.address}} </td>
</tbody>
</table>
</div>
//Step2 add a directive which will leverage the $last property of ngRepeat,
//listen to the when $last is called so that it can invoke a even which inturn can load the javascript
Application.Emp.directive('renderOnFinish',[function() {
return function(scope, element, attrs) {
if (scope.$last){
scope.$emit('event:table-loading-over');
}
}
}])
//Step3 - listen to even in the empController
//now we are using the timeout in this, reason is it calls the $apply method which will notify the angular about the dom change
$scope.$on('event:table-loading-over',function(event){
$timeout(function(){
loadCustomJS();
});
});
//Step4 - use the directive in the html
<div ng-controller="empController" >
<table id="myTable" class="emp-table" render-on-finish>
<td>.. </td>
</table>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment