我正处理angularjs
中的分页,其中我曾经从服务器端检索数据,因此它用于按照每个ajax调用加载一些数据集。我使用ui-bootstrap
库来实现pagination
。
问题是,当我从任何其他页面转到任何页面时,当我按照ui-bootstrap
库按下前一个按钮时,它工作正常但按下browser back button
时它会直接重定向到主页(/ dashboard)而不是显示以前的表格内容。
var app=angular.module('myApp', ['ngRoute','ngCookies','ui.bootstrap']);
console.log("in appnew.js")
app.config(function($routeProvider,$locationProvider) {
$locationProvider.html5Mode(true);
$routeProvider
.when('/',{
templateUrl: 'login_admin.html'
})
.when('/dashboard',{
templateUrl: 'dashboard.html',
controller: 'userController',
authenticated: true
})
.when('/logout',{
templateUrl: 'logout_admin.html',
controller: 'userController',
authenticated: true
})
.when('/add',{
templateUrl: 'add_brands.html',
controller: 'dashCtrl',
authenticated: true
})
.otherwise({
redirectTo: "/"
});
});
app.factory('userModel', function($http,$cookies,$location){
var userModel={};
userModel.login= function(loginfrm){
console.log("loginfrm"+loginfrm.user_name);
$http.post("http://.........../admin_login",loginfrm).
success(function(response){
if (response.success == 1) {
console.log("Password Matched");
$cookies.put('user_name',loginfrm.user_name);
$cookies.put('token',response.token);
$location.path('/dashboard');
} else if (response.success == -1) {
console.log("Password Not Matched");
} else {
console.log("Sorry there is some error");
}
/*console.log('$scope.dynamic1: %j', $scope);*/
console.log("response success: %j",response)
}).
error(function(response){
console.log("response error:",response);
});
};
userModel.getAuthStatus = function(){
var status = $cookies.get('user_name');
console.log('status: %j', status);
if(status){
return true;
}
else{
return false;
}
};
console.log('userModel.getAuthStatus'+userModel.getAuthStatus());
userModel.doUserLogout = function(){
$cookies.remove('user_name');
$cookies.remove('token');
}
console.log("userModel: %j",userModel);
return userModel;
});
app.controller('userController',function($scope,$location,userModel,$routeParams,$route){
angular.extend($scope,{
login: function(adminfrm){
var data={
user_name: $scope.admin.name,
password: $scope.admin.password
};
userModel.login(data);
},
logout: function(){
userModel.doUserLogout();
$location.path('/');
}
});
});
app.filter('startFrom',function(){
return function(data,start){
if(data != null) {
console.log("data fetched");
console.log('data: %j',data);
return data.slice(start);
}
else if(data == null){ console.log("data is null"); }
}
});
app.controller('dashCtrl', function($scope,$http,$cookies,$filter){
$scope.users = [];
$scope.totalItems=1000;
$scope.pageSize=10;
console.log("$scope.current page1000....................."+$scope.currentPage);
$scope.currentPage=1;
$scope.callbackPage=1;
/*$scope.currentPage=1;*/
$scope.mobRegex=/^(?:(?:\+|0{0,2})91(\s*[\-]\s*)?|[0]?)?[789]\d{9}$/;
$scope.regdigit = /^\d+$/;
$scope.check= function(){
var mobilereg = /^(?:(?:\+|0{0,2})91(\s*[\-]\s*)?|[0]?)?[789]\d{9}$/;
var digitreg = /^\d+$/;
if(mobilereg.test($scope.dash.mobile) && digitreg.test($scope.dash.mobile)){
$scope.dashform1hide = true;
/*$scope.showDetails=false;*/
var dashno={
mobile_no: $scope.dash.mobile,
user_name: $cookies.get('user_name'),
token: $cookies.get('token')
};
$http.post('http://............/get_user_info', dashno).
success(function(data,status,header,config){
console.log("data: %j", data);
if(data.success == 1){
$scope.showDetails=true;
$scope.email_id = data.email_id;
$scope.user_name = data.user_name;
$scope.default_vpa = data.default_vpa;
$scope.def_vpa_name = data.def_vpa_name;
fetchedTrans();
} else if (data.success == -1) {
//mobile no. not exist
document.getElementById("msgfornx")="Mobile no. does not exist";
}
else{
console.log('check in else')
}
}).
error(function(data,status,header,config){
console.log("data"+data);
console.log('status'+status);
console.log('header'+header);
console.log('config: %j',config);
alert("something wrong");
})
};
}
$scope.pageChanged = function(currentPage) {
console.log('Page changed to: ' + $scope.currentPage);
console.log('psge value is: ', currentPage);
$scope.callbackPage = currentPage;
/*$scope.currentPage=page;
var begin = (($scope.currentPage - 1) * $scope.pageSize)
, end = begin + $scope.pageSize;
$scope.users = $scope.users.slice(begin, end);*/
console.log("$scope.users[$scope.callbackPage*10]"+$scope.users[$scope.callbackPage*10]);
if($scope.users[($scope.callbackPage-1)*10]==undefined){
//$scope.users[$scope.callbackPage]=$scope.users[$scope.callbackPage*10]
fetchedTrans();
$window.history.back();
}
else{
console.log("data came aleady");
}
};
var fetchedTrans = function(){
var data={
mobile_no: $scope.dash.mobile,
user_name: $cookies.get('user_name'),
token: $cookies.get('token'),
page:$scope.callbackPage-1
};
/*data.page=$scope.callbackPage;*/
console.log("new page value is"+data.page);
console.log('sending data %j', data);
console.log('$scope.dash.mobile: ', $scope.dash.mobile);
$http.post('http://.........../get_user_trans', data).
success(function(data,status,header,config){
console.log("data: %j", data);
console.log("data,status"+data.transactions[0].status);
if(data.success == 1){
if(($scope.callbackPage-1)==0){
$scope.totalItems = data.total_trans;
$scope.users = data.transactions;
}
else{
for(var i=($scope.callbackPage-1)*10; i<=(($scope.callbackPage-1)*10)+9; i++){
$scope.users[i] = data.transactions[i-(($scope.callbackPage-1)*10)];
}
}
console.log("data in array2: %j",$scope.users);
console.log('totalItems is: '+$scope.totalItems);
console.log("current page is: "+$scope.currentPage);
} else if (data.success == -1) {
//mobile no. not exist
}
else{
console.log('check in else')
}
}).
error(function(data,status,header,config){
console.log("data"+data);
console.log('status'+status);
console.log('header'+header);
console.log('config: %j',config);
alert("something wrong");
})
}
});
app.run(["$rootScope",'$location','userModel','$routeParams','$route', function($rootScope,$location,userModel,$window,$routeParams,$route){
$rootScope.$on('$routeChangeSuccess', function(event, next, current){
console.log("event: %j",event);
console.log("next: %j",next);
console.log('current: %j',current);
$rootScope.actualLocation = $location.path();
console.log('$rootScope.actualLocation'+$rootScope.actualLocation);
//$location.url('/dashboard').replace(undefined);
console.log('$location.url()'+$location.url());
console.log('$location.url().replace()'+$location.url().replace());
if (next.$$route.authenticated) {
console.log("next.$$route.authenticated"+next.$$route.authenticated);
console.log('userModel.getAuthStatus app.run if 1: %j',userModel.getAuthStatus());
if (!userModel.getAuthStatus()) {
console.log("getAuthStatus ",!userModel.getAuthStatus);
console.log('userModel.getAuthStatus app.run if 1(1)'+userModel.getAuthStatus());
$location.path('/');
}
}
if (next.$$route.originalPath =='/') {
console.log("next.$$route.originalPath "+next.$$route.originalPath);
/*console.log("current.$$route.originalPath "+$route.current.$$route.originalPath);*/
if (userModel.getAuthStatus()) {
console.log("current "+current);
console.log("next "+next);
next.$$route.originalPath = '/dashboard';
$location.path(next.$$route.originalPath);
$location.url('/dashboard').replace(undefined);
//current.$$route.originalPath = next.$$route.originalPath;
//current.$$route.originalPath = '';
} else {
}
}
});
}]);