4

I am dealing with pagination in angularjs in which i used to retrieved data from server side so that it used to load some set of data as per every ajax call. I used ui-bootstrap library to achieve pagination. the problem is, when i go to any page from any other page and when i press previous button as per ui-bootstrap library, it works fine but when pressing browser back button it redirect to home page(/dashboard) directly instead of showing previous table content.

 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 {

                            }
            }
    });
    }]);
Sunny
  • 577
  • 6
  • 20
  • Add an id to the back button in the table or try to disable the browser's back button https://stackoverflow.com/questions/17962130/restrict-user-to-refresh-and-back-forward-in-any-browser – Hema Nandagopal Jul 03 '17 at 13:00
  • Could you please check this link "https://stackoverflow.com/questions/24200196/on-back-button-click-execute-a-function" and do trigger on previous page button click event – Mohamed Navas Jul 03 '17 at 13:12
  • @MohamedNavas i fi wish to play with `history()` method then how to store the previous view of same html page can be stored in object. can you please give me demo for the same. – Sunny Jul 04 '17 at 10:26

0 Answers0