0

I have created one custom directive to add active class for clicked li in menu list based on url.

.directive('addActive', [function() {
    return{
        ...
        link : function(scope, element, attrs){

                pageUrl = location.pathname;
                $('#sidebar-left .nav li.active').removeClass("active");
                if (pageUrl) {
                    debugger
                    console.log($('.nav li:has(a[href="' + pageUrl + '"])'))
                    $('.nav li:has(a[href="' + pageUrl + '"])').addClass("active");
                }
        }
    }
}]);

directive code defines that when a menu is clicked, active class should be added for current url. how can I use this directive in html code?

Html:

 <div id="sidebar-left" class="col-xs-2 col-sm-2">
            <ul class="nav main-menu">
                <li class="dropdown ng-scope" ng-repeat="parent in menu">
                    <a href="/employee/Home" class="dropdown-toggle" ng-click="tabName(parent.name)">
                        <i class="fa fa-home"></i>
                        <span class="hidden-xs ng-binding">Home</span>
                    </a>                       
                </li>
                <li class="dropdown ng-scope" ng-repeat="parent in menu">
                    <a href="/documents/doc_details" class="dropdown-toggle" ng-click="tabName(parent.name)">
                        <i class="fa fa-file-text"></i>
                        <span class="hidden-xs ng-binding">Documents</span>
                    </a>                       
                </li>
                <li class="dropdown ng-scope" ng-repeat="parent in menu">
                    <a href="#" class="dropdown-toggle" ng-click="tabName(parent.name)">
                        <i class="fa fa-money"></i>
                        <span class="hidden-xs ng-binding">Pay &amp; Benifits</span>
                    </a>
                    <ul class="dropdown-menu">
                       <li ng-repeat="child in parent.children" class="ng-scope">
                            <a href="/pay/paymanagement" ng-click="tabName(child.name)" class="ng-binding">slips</a>
                        </li>
                    </ul>
                </li>                    
            </ul>
        </div>

Please guys any help?

laz
  • 281
  • 8
  • 18
  • 1
    You should simply use ng-class on your component, instead of a new directive. https://docs.angularjs.org/api/ng/directive/ngClass – Deblaton Jean-Philippe Jan 22 '18 at 09:20
  • Above comment is right. If you still want to know how to use directives, have a look at the docs. https://docs.angularjs.org/guide/directive – JSON Derulo Jan 22 '18 at 09:24
  • I aggre ng-class is the way to do here . What you should probably do is hold a list with boolean values for your
  • elements and onClick make its value true( this should be associated with a condition on ng-class). Also every time you click an element you should make false the rest of list first also check this solution using $index https://stackoverflow.com/questions/20902583/angularjs-best-practices-on-adding-an-active-class-on-click-ng-repeat
  • – Panos K Jan 22 '18 at 09:25
  • @DeblatonJean-Philippe @D.Simon if I use `ng-click` in `li`, each click of list ,page will get refreshed.so `active` class cant be added.Am I right? – laz Jan 22 '18 at 10:19
  • @PanosK you are correct but for li each click my page gets refreshed.Thats why am trying to do using current url. – laz Jan 22 '18 at 10:30
  • @laz your condition on ng-class should be based on your current url so you probably want to keep this on a scope variable in your controller, using jquery addClass is definitely not the correct way to do this – Panos K Jan 22 '18 at 10:50
  • @PanosK can you please give some sample code for my condition.please – laz Jan 22 '18 at 10:51
  • ng-class="{'active': $location.path() == 'pathToAddActiveCondition' }" not tested but should work. Iwill make a plunkr when i find some time – Panos K Jan 22 '18 at 11:00
  • @laz AngularJS is for Single Page Applications (SPA). Those shouldn't refresh, only render data (on $digest cycle for angularjs) – Aleksey Solovey Jan 22 '18 at 11:01