How to implement history.back() in angular.js

前端 未结 10 1632
南笙
南笙 2020-11-27 09:37

I have directive which is site header with back button and I want on click to go back to the previous page. How do I do it in the angular way?

I have tried:

相关标签:
10条回答
  • 2020-11-27 10:18

    Angular routes watch the browser's location, so simply using window.history.back() on clicking something would work.

    HTML:

    <div class="nav-header" ng-click="doTheBack()">Reverse!</div>
    

    JS:

    $scope.doTheBack = function() {
      window.history.back();
    };
    

    I usually create a global function called '$back' on my app controller, which I usually put on the body tag.

    angular.module('myApp').controller('AppCtrl', ['$scope', function($scope) {
      $scope.$back = function() { 
        window.history.back();
      };
    }]);
    

    Then anywhere in my app I can just do <a ng-click="$back()">Back</a>

    (If you want it to be more testable, inject the $window service into your controller and use $window.history.back()).

    0 讨论(0)
  • 2020-11-27 10:27

    Another nice and reusable solution is to create a directive like this:

    app.directive( 'backButton', function() {
        return {
            restrict: 'A',
            link: function( scope, element, attrs ) {
                element.on( 'click', function () {
                    history.back();
                    scope.$apply();
                } );
            }
        };
    } );
    

    then just use it like this:

    <a href back-button>back</a>
    
    0 讨论(0)
  • 2020-11-27 10:28

    In case it is useful... I was hitting the "10 $digest() iterations reached. Aborting!" error when using $window.history.back(); with IE9 (works fine in other browsers of course).

    I got it to work by using:

    setTimeout(function() {
      $window.history.back();
    },100);
    
    0 讨论(0)
  • 2020-11-27 10:32

    Ideally use a simple directive to keep controllers free from redundant $window

    app.directive('back', ['$window', function($window) {
            return {
                restrict: 'A',
                link: function (scope, elem, attrs) {
                    elem.bind('click', function () {
                        $window.history.back();
                    });
                }
            };
        }]);
    

    Use like this:

    <button back>Back</button>
    
    0 讨论(0)
  • 2020-11-27 10:33

    You need to use a link function in your directive:

    link: function(scope, element, attrs) {
         element.on('click', function() {
             $window.history.back();
         });
     }
    

    See jsFiddle.

    0 讨论(0)
  • 2020-11-27 10:35

    In AngularJS2 I found a new way, maybe is just the same thing but in this new version :

    import {Router, RouteConfig, ROUTER_DIRECTIVES, Location} from 'angular2/router'; 
    
    (...)
    
    constructor(private _router: Router, private _location: Location) {}
    
    onSubmit() {
        (...)
        self._location.back();
    }
    

    After my function, I can see that my application is going to the previous page usgin location from angular2/router.

    https://angular.io/docs/ts/latest/api/common/index/Location-class.html

    0 讨论(0)
提交回复
热议问题