I use ng-table for the administrator and user with the same controller, with the same view, but loaded data with a different url, but when receiving data from the cache, it reloads the data from the cache (which I want to clear when user logs out)
controller
myApp.controller('listArticle', ['$scope', '$filter', 'ngTableParams', 'nameService', '$rootScope', '$location', '$timeout', '$cookieStore', 'flashService', '$templateCache', function ($scope, $filter, ngTableParams, nameService, $rootScope, $location, $timeout, $cookieStore, flashService, $templateCache) { //$templateCache.removeAll(); $scope.success = {}; $scope.article = {}; $scope.article.edit = '-'; $scope.article.approve = '-'; $scope.article.view = 'view'; $scope.showAlert = true; flashService.displayAlertMessages(); $scope.tableParams = new ngTableParams( { page: 1, // show first page count: 10, // count per page sorting: {name: 'asc'} }, { total: 0, // length of data getData: function ($defer, params) { nameService.getData($defer, params, $scope.filter); }, counts: [], paginationMaxBlocks: 13 }); $scope.$watch("filter.$", function () { $scope.tableParams.reload(); }); }]);
Service
myApp.service("nameService",['$http','$filter','$cookieStore', '$rootScope', function($http, $filter, $cookieStore, $rootScope){ function filterData(data, filter) { return $filter('filter')(data, filter); } function orderData(data, params) { return params.sorting() ? $filter('orderBy')(data, params.orderBy()) : filteredData; } function sliceData(data, params) { return data.slice((params.page() - 1) * params.count(), params.page() * params.count()); } function transformData(data,filter,params) { return sliceData( orderData( filterData(data,filter), params ), params); } var service = { cachedData:[], getData:function($defer, params, filter) { if(service.cachedData.length>0) { var filteredData = filterData(service.cachedData,filter); transformedData = sliceData(orderData(filteredData,params),params); params.total(filteredData.length); $defer.resolve(transformedData); } else { var id = $cookieStore.get('userId'); if($rootScope.role == 1) { var url = "article/serverside/fetch-pending-list.php"; var data = ""; } else { var url = "article/serverside/fetch-list.php"; var data = {id:id}; } $http.post(url,data) .success(function(resp) { var i=0; for(i=0; i<resp.length; i++) { resp[i].status = parseInt(resp[i].status); resp[i].category = parseInt(resp[i].category); if($rootScope.role > 1) resp[i].edit = (resp[i].status == 1)?"Edit":""; else{ resp[i].approve = (resp[i].status == "2")?"Approve/Reject":""; } var j=0; var k=0; for(j=0;j<statusList.length;j++){ if(statusList[j]['id'] == resp[i].status) resp[i].status = statusList[j]['title']; } for(k=0;k<categories.length;k++){ if(categories[k]['id'] == resp[i].category) resp[i].category = categories[k]['title']; } } angular.copy(resp,service.cachedData); params.total(resp.length); var filteredData = $filter('filter')(resp, filter); transformedData = transformData(resp,filter,params); $defer.resolve(transformedData); }); } } }; return service;
}]);
Note if(service.cachedData.length>0) The same data is placed here as on both inputs. Also, if I save the data form and redirect them to the ng table, the list is not updated, because it loads data from the cache not from the source.