I'm trying to display a datatable using angular and jquery datatable but so far the datatable stays empty after applying the datatable function.
I've read that the best way is to use a directive but I can't get it working. This only I've manage to get it working is by applying a timeout with 100 ms (time out less than 100 didn't work)
What I'd like to do is to apply the datatable function after the DOM is rendered. I'm sure someone has managed to do that ;)
userController.js
myApp.controller('UserController', ['$scope', 'User',
function ($scope, User) {
User.query(function(data) {
$scope.users = data;
}, function(errorData) {
});
}]);
datatableSetup.js
myApp.directive('datatableSetup', ['$timeout',
function ($timeout) {
return {
restrict: 'A',
link: function (scope, elm, attrs) {
$timeout(function() {
elm.dataTable();
}, 100);
}
}
}]);
user.html
<table datatable-setup="" class="table table-bordered table-striped">
<thead>
<tr>
<th>Username</th>
<th>Roles</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="user in users">
<td>{{user.username}}</td>
<td>
<ul>
<li ng-repeat="role in user.roles">
{{role}}
</li>
</ul>
</td>
</tr>
</table>