Search code examples
angularjsbrowser-history

AngularJS: Disable html button if history is empty


In my AngularJS application I have implemented a html button with a "go back"-functionality . In case the history is empty (because the user hasn't changed the path since page load) I want to hide/disable it.

Is there a way to detect if the history is empty with angular?


Solution

  • You can even check $window service of angular js. It is completely same as window object of javascript.

    The history object contains the URLs visited by the user (within a browser window).

    The history object is part of the window object and is accessed through the window.history property.

    If you want to see whether history is empty or not you should use

    if( $window.history.length <= 1 ) {
      //Internet Explorer and Opera start at 0, while Firefox, Chrome, and Safari start at 1
      console.log("empty");
    }
    

    Please make sure you've injected $window service in your controller. Some couple of functions of history object are here. You can write more lines to detect browser first and after then apply your conditions in angular way. Please check this post