0

I want active class only on the a tag that i click on, rest should show 'inactive class':

app.controller("myCtrl", function($scope, $log){
       $scope.activeact=false;
       $scope.makeactive=function(){

       $scope.activeact=true;
    }
});


<ul>
    <li><a href="#" ng-click="makeactive()" ng-class="activeact ? 'active' : 'inactive'">one</a></li>
    <li><a href="#" ng-click="makeactive()" ng-class="activeact ? 'active' : 'inactive'">Two</a></li>
    <li><a href="#" ng-click="makeactive()" ng-class="activeact ? 'active' : 'inactive'">Three</a></li>
</ul>

pls help

4
  • Is number of list elements, i.e, number of <li> tags fixed? Commented Mar 22, 2017 at 13:33
  • it is menu? routing between pages? Commented Mar 22, 2017 at 13:34
  • yes, they are fixed, you can give example to ng-repeat later, after answer for fixed li's Commented Mar 22, 2017 at 13:35
  • there is no routing Commented Mar 22, 2017 at 13:35

4 Answers 4

2

Considering the number of list elements are fixed and small for a easy solution;

app.controller("myCtrl", function($scope, $log){

       $scope.makeactive=function(num){
            $scope['li'+num]=true;
       }
});

And the HTML is:

<ul>
   <li><a href="#" ng-click="makeactive(1)" ng-class="li1 ? 'active' : 'inactive'">one</a></li>
   <li><a href="#" ng-click="makeactive(2)" ng-class="li2 ? 'active' : 'inactive'">Two</a></li>
   <li><a href="#" ng-click="makeactive(3)" ng-class="li3 ? 'active' : 'inactive'">Three</a></li>
</ul>

Obviously it is not a generalised solution, but for small elements this is an easier one.

Sign up to request clarification or add additional context in comments.

Comments

1

Here's what you can do! Make use of ng-repeat using following array:

$scope.numbers = ["one", "two", "three"];

Now, your ng-repeat on <li> would look like this:

<li ng-repeat="num in numbers">
  <a href="#" ng-click="makeactive($index)" 
     ng-class="activeact{{$index}} ? 'active' : 'inactive'"
     ng-bind="num">
  </a>
</li>

Now, on click of your links, active/inactive will toggle.

Here's the working code snippet!

angular.module("myApp", [])
  .controller("myCtrl", function($scope, $log) {
    $scope.activeact = false;
    $scope.numbers = ["one","two","three"]
    $scope.makeactive = function(index) {
      $scope["activeact"+index] = !Boolean($scope["activeact"+index]);
    }
  });
.active {
  color: green
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<body ng-app="myApp" ng-controller="myCtrl">
  <ul>
    <li ng-repeat="num in numbers"><a href="#" ng-click="makeactive($index)" ng-class="activeact{{$index}} ? 'active' : 'inactive'">{{num}}</a></li>
  </ul>
</body>

Comments

0

Use can use different code like this

<ul>
<li><a href="#" ng-click="makeactive(1)" ng-class="selectedindex==1 ? 'active' : 'inactive'">one</a></li>
<li><a href="#" ng-click="makeactive(2)" ng-class="selectedindex==2 ? 'active' : 'inactive'">Two</a></li>
<li><a href="#" ng-click="makeactive(3)" ng-class="selectedindex==3 ? 'active' : 'inactive'">Three</a></li>
</ul>

In controller

$scope.makeactive=function(index){    
  $scope.selectedindex=index;
}

Comments

0

you can use ui-router :

<ul class="nav nav-pills nav-justified">
    <li ui-sref-active="active"><a ui-sref="home">home</a></li>
    <li ui-sref-active="active"><a ui-sref="about">about</a</li>
    <li ui-sref-active="active"><a ui-sref="contact">contact</a></li>
</ul>

for tutorials here : github and Scotch

examples of ui router : exaple 1 and codepen

you can do with ui-router the same without ng-class or ng-repeat

Comments

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.