1

I am very new to Angular and I have this piece of code:

<nav ng-controller="navController">
  <ul>
    <li ng-class="{'active': location('/') || location('/homepage')}">
        <a href="puppies">view puppies</a>
    </li>
  </ul>
</nav>

With the corresponding controller:

obApp.controller('navController', function($scope, $location) {
    $scope.location = function(loc){
        if(loc == $location.path()){
            return true;
        }else{
            return false;
        }
    }
})

Now everything works fine. When I'm on the homepage the list element gets the class active added to it, HOWEVER there is also the angular ng-class directive code shown in the source, like so (this is the HTML source output of the browser):

<li ng-class="{'active': location('/') || location('/homepage')}" class="active">
    <a href="puppies">view puppies</a>
</li>

Should the list element not look like: <li class="active"><a href="puppies">view puppies</a></li> ? Logic would dictate that it should look like it is now, because ng-controller="x" shows in the source as it is as well. However that's a hook if you will. The other one's an expression. It looks funky and I'm not sure if it's ok or not. Please, if you answer, detail your answer.

2 Answers 2

1

There is nothing wrong with your code, ng-class directive doesn't remove the attribute ng-class from the element when the expression evaluates to true, since you could have more than one expression/class in your ng-class attribute value.

Additionally if the directive did remove the attribute (ng-class) angular would have no way of knowing that you have applied that directive to the element. For example, your element had ng-if on it, it would be removed from the DOM and appended conditionally.

Example

ng-class="{'active': location('/') || location('homepage'), 'disabled': location('private'))}"
Sign up to request clarification or add additional context in comments.

1 Comment

Thanx. I guess i was expecting the removal of everything in parentheses and only 'ng-class' to remain.
1

Such usage of ng-class is non-effective because $scope.location method will be called at least twice (for location('/') and location('homepage')) each digest loop. There could be a lot of digest loops due to balancing mechanism.

Better approach is to calculate if element is active on location change.

Controller:

$scope.$on('$locationChangeSuccess', function(){
   $scope.isHomePage =  location('/') || location('/homepage');
});

Markup:

<nav ng-controller="navController">
  <ul>
    <li ng-class="{'active': isHomePage}">
        <a href="puppies">view puppies</a>
    </li>
  </ul>
</nav>

1 Comment

True. Just threw it together since i'm experimenting at this point. Upvoted. Thanx.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.