I want to implement the pagination in angular js. I am new to angular js.
I get this error in browser:
Error: [$injector:unpr] http://errors.angularjs.org/1.3.15/$injector/unpr?p0=ngTableParamsProvider%20%3C-%20ngTableParams%20%3C-%20allUsersCtrl
I have included these files at the HEAD section of layout view:
<!-- angular js--->
<script src="<?php echo base_url();?>angular/js/angular.min.js"></script>
<script src="<?php echo base_url();?>angular/js/angular-route.min.js"></script>
<!-- flash msg -->
<script src="<?php echo base_url();?>angular/js/angular-flash.js"></script>
<!-- flash msg -->
<!--loading bar-->
<script src="<?php echo base_url();?>angular/js/loading-bar.min.js"></script>
<script src="<?php echo base_url();?>angular/js/angular-animate.min.js"></script>
<script src="<?php echo base_url();?>angular/js/ng-table.min.js"></script>
<!--loading bar-->
<script src="<?php echo base_url();?>angular/js/demo_angular.js"></script>
<script src="<?php echo base_url();?>angular/js/app.js"></script>
This is my table in partial html:
<table ng-table="tableParams" class="table table-striped table-bordered table-hover" id="table_all_users">
<thead>
<tr>
<th width="20%"><input type="checkbox" name="chk_all" id="chk_all" onchange="checkAll(this)" /> Select</th>
<th width="15%">Sr. No.</th>
<th width="35%"><a href="" ng-click="reverse=!reverse;order('username', reverse)">User Name</a></th>
<th width="25%"><a href="" ng-click="reverse=!reverse;order('fname', reverse)">First Name</a></th>
<th width="25%"><a href="" ng-click="reverse=!reverse;order('lname', reverse)">Last Name</a></th>
<th width="15%"><a href="" ng-click="reverse=!reverse;order('mobile', reverse)">Mobile</a></th>
<th width="15%"><a href="" ng-click="reverse=!reverse;order('email', reverse)">Email</a></th>
<th width="15%">Action</th>
</tr>
</thead>
<tbody ng-init="get_users()">
<tr class="gradeX" ng-repeat="user in allUsers | filter:query">
<td width="20%"><input type="checkbox" name="list[]" class="chk_all" value="" id="" onclick="uncheck_select_all(this);" /></td>
<td width="15%">{{ $index + 1 }}</td>
<td width="35%">{{ user.username }}</td>
<td width="25%">{{ user.fname }}</td>
<td width="25%">{{ user.lname }}</td>
<td width="15%">{{ user.mobile }}</td>
<td width="15%">{{ user.email }}</td>
<td width="15%"><a title="Edit User" href="#/edit_user/{{user.user_id}}" ><span class="fa fa-pencil"></span></a> | <a title="Delete User" id="" style="cursor:pointer;" ng-click="delete_user(user.user_id)" ><span class="fa fa-trash-o"></span></a></td>
</tr>
</tbody>
</table>
This is the script.
var angularControllers = angular.module('angularControllers', ['flash']);
angularControllers.controller('allUsersCtrl', ['$scope','$http','$filter','Flash','ngTableParams', function($scope,$http,$filter,Flash,ngTableParams){
$scope.get_users = function()
{
$http.get("../admin/users/angular_all_users").success(function(data)
{
$scope.allUsers = data;
});
}
//for sorting by table column
var orderBy = $filter('orderBy');
$scope.order = function(predicate, reverse)
{
$scope.allUsers = orderBy($scope.allUsers, predicate, reverse);
}
//table pagination
$scope.tableParams = new ngTableParams({
page: 1, // show first page
count: 10 // count per page
}, {
total: data.length, // length of data
getData: function ($defer, params) {
$defer.resolve(data.slice((params.page() - 1) * params.count(), params.page() * params.count()));
}
})
}]);
When I am adding here as: ['flash','ngTable'] still it gives error : data is not defined
Also after trying to implement pagination my data is not going to display, only header row is display.
Please suggest me any solution or any link that will guid step by step for pagination
My advice to you is always to initialize the ng-table in $http.get. It must be like:
With this "best practice" you will always avoid the problem that
data
with it's properties is undefined.Don't forget to include
ngTable
in your module.Hope this will push you forward.
Best regards!