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 () {

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>

And that’s it.


Leave a Reply

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

You are commenting using your 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