title: To load the very previous page after closing the modal
body: <p>I am new to angularjs and i have created page with two links
  1)For Modal popup
  2)Is a jsp page
  And my issue is how i can load the very previous page after closing of the modal popup opened.
  Now it is directly going back to the home or index page. I want it to go back to the previous where the modal popup was invoked?
  Can any one help me to overcome this?
  And thanks in advance
  Code is written below
  `</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en" ng-app="app"&gt;
  
  &lt;head&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;script src="css/style.css"&gt;&lt;/script&gt;
    &lt;script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.0/angular.js"&gt;&lt;/script&gt;
    &lt;script src="//cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.11/angular-ui-router.js"&gt;&lt;/script&gt;
    &lt;script src="//cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.11.2/ui-bootstrap-tpls.js"&gt;&lt;/script&gt;
    &lt;link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.1/css/bootstrap.css" rel="stylesheet"&gt;
    &lt;script&gt;
  
      // init the app with ui.router and ui.bootstrap modules
      var app = angular.module('app', ['ui.router', 'ui.bootstrap']);
  
      // make back button handle closing the modal
      app.run(['$rootScope', '$modalStack',
        function($rootScope, $modalStack) {
          $rootScope.$on('$stateChangeStart', function() {
            var top = $modalStack.getTop();
            if (top) {
              $modalStack.dismiss(top.key);
            }
          });
        }
      ]);
  
      // configure the stateProvider
      app.config(['$stateProvider', '$urlRouterProvider',
        function($stateProvider, $urlRouterProvider) {
  
          // default page to be "/" (home)
          $urlRouterProvider.otherwise('/');
  
          // configure the route states
          $stateProvider
  
            // define home route "/"
            .state('home', {
              url: '/'
            })
            .state('profile', {
              url: '/profile',
              templateUrl:'newjsp2.jsp'
            })
  
            // define modal route "/modal"
            .state('modal', {
              url: '/modals',
  
              // trigger the modal to open when this route is active
              onEnter: ['$stateParams', '$state', '$modal',
                function($stateParams, $state, $modal) {
                  $modal
  
                    // handle modal open
                    .open({
                      abstract: true,
                      templateUrl: 'modalwindow.jsp',
                      backdrop: false,
                      controller: ['$scope',
                        function($scope) {
  
                          // handle after clicking Cancel button
                          $scope.cancelModal = function() {
                            $scope.$dismiss();
                          };
                          // close modal after clicking OK button
                          $scope.ok = function() {
                            $scope.$close(true);
                          };
                        },
  
                      ]
                    })
  
                    // change route after modal result
                    .result.then(function() {
                      // change route after clicking OK button
                      $state.transitionTo('home');
                    }, function() {
                      // change route after clicking Cancel button or clicking background
                      $state.transitionTo('home');
                    });
  
                }
              ]
  
            })
            .state('modals', {
              url: '/modals2',
              modal:true,
              // trigger the modal to open when this route is active
              onEnter: ['$stateParams', '$state', '$modal',
                function($stateParams, $state, $modal) {
                  $modal
  
                    // handle modal open
                    .open({
                      templateUrl: 'modalwindow2.jsp',
                      backdrop: false,
                      controller: ['$scope',
                        function($scope,$state) {
                          // handle after clicking Cancel button
                          $scope.cancelModal = function() {
                            $scope.$dismiss();
  
                          };
                          // close modal after clicking OK button
                          $scope.ok = function() {
                            $scope.$close(true);
                          };
                        },
  
                      ]
                    })
  
                    // change route after modal result
                    .result.then(function() {
                      // change route after clicking OK button
  
                      $state.transitionTo('home');
                    }, function() {
                      // change route after clicking Cancel button or clicking background
                      $state.transitionTo('profile');
                    });
  
                }
              ]
  
            });
        }
      ])
      .run(function($rootScope, $location, $state) {
    $rootScope.$location = $location;
    alert(location)
    $rootScope.$state = $state;
  });
    &lt;/script&gt;
  &lt;/head&gt;
  
  &lt;body style="background-color: chocolate"&gt;
  
      &lt;a href="#/modals" class="btn btn-default"&gt;POPUP----&gt;1&lt;/a&gt;
      &lt;a href="#/modals2" class="btn btn-default"&gt;POPUP--&gt;2&lt;/a&gt;
      &lt;a href="#/profile" class="btn btn-default"&gt;Profile!&lt;/a&gt;
  
    &lt;div ui-view&gt;&lt;/div&gt;
  
  &lt;/body&gt;
  &lt;style&gt;
       .modal-dialog{
            width: 100% !important;
            margin: 0px !important;
        }
        .modal-body{
            padding-bottom:488px;
        }
  &lt;/style&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>'</p>