AngularJS directive for windows.history.back()

In case you want to implement windows.history.back() in a pure AngularJS way, this is how you do it.

First, create a simple directive:

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

Then, in your HTML, you add the directive to an element – like so:

<a ng-href="#" back-button class="link-back">
<span class="icon icon-arrow-left"></span>
</a>

And that’s it.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s