angularjs

AngularJS: How to conditionally display an element outside of ng-view?


Considering the following Angular 1.X index.html file:

<div ng-app="app" class="hero-unit">
    <a ng-show="!isHomePage" href="#!/home">Home</a>
    <div class="container-fluid">
        <div ng-view></div>
    </div>
</div>

I would like to display the element <a ng-show="!isHomePage" href="#!/home">Home</a> only when the location is not the home page? How should I proceed? Should I map this element on a dedicated controller as mainController? Is that a good practice?


Solution

  • Ways to achieve this :

    To get the current path you can use :

    app.run(function ($rootScope) {
        $rootScope.$on('$routeChangeSuccess', function (e, current, pre) {
            console.log(current.originalPath); // Do not use $$route here it is private
        });
    });
    

    OR

    $location.path()