2

This is the html code for the buttons, taskfilter is the filter of how the buttons work on click and the class name is 'sel'

<a class="clear-completed" ng-click="taskfilter = 1" ng-class="{'sel':enabled}">
  <span>show completed</span>.
</a>
<a class="clear-completed" ng-click="taskfilter = 2" ng-class="{'sel':enabled}">
  <span>show to do</span>.
</a>
<a class="clear-completed" ng-click="taskfilter = 0" ng-class="{'sel':enabled}">
  <span>show all</span>.
</a>  

This is the code I used to add the class scope with the button on click removed the index in html because it wont work

$scope.taskfilters = 0;
$scope.taskfilter = function(index) {
    $scope.taskfilters = index;
};

4 Answers 4

1
<a class="clear-completed" ng-click="taskfilters = 1" ng-class="{'sel':taskfilters == 1}">
     <span>show completed</span>.
</a>
<a class="clear-completed" ng-click="taskfilters = 2" ng-class="{'sel':taskfilters} == 2">
     <span>show to do</span>.
</a>
<a class="clear-completed" ng-init="taskfilters = 0" ng-click="taskfilters = 0" ng-class="{'sel':taskfilters == 0}">
     <span>show all</span>.
</a>
Sign up to request clarification or add additional context in comments.

2 Comments

but how do i have ' taskfilter = 0 button' with active class on inital value. it only shows no if i click on the button but i want to have the first button on reload with the class and if i click the other ones to have them red instead of the first button
Another option is, you can write $scope.taskfilters = 0 at very first line of controller to initialize value. Otherwise ng-init is just fine.
1

You can use ngClass directive

If the expression evaluates to an object, then for each key-value pair of the object with a truthy value the corresponding key is used as a class name.

<a ng-class="{'active' : taskfilter == 0}" ng-click="taskfilter = 0">

1 Comment

but how do i have ' taskfilter = 0 button' with active class on inital value. it only shows no if i click on the button but i want to have the first button on reload with the class and if i click the other ones to have them red instead of the first button
1
<a class="clear-completed" ng-click="taskfilters = 1" ng-class="{'sel':taskfilters == 1}">
     <span>show completed</span>.
</a>
<a class="clear-completed" ng-click="taskfilters = 2" ng-class="{'sel':taskfilters} == 2">
     <span>show to do</span>.
</a>
<a class="clear-completed" ng-click="taskfilters = 0" ng-class="{'text-primary':!taskfilters, 'sel':taskfilters == 0}">
     <span>show all</span>.
</a>

1 Comment

i only need it to be orange on reload so you see that you are at the beginning, and if uou want to check the other ones the class switches to the other ones. if youo know this i wil make your answer the correct one
0
var app = angular.module("ap",[]);

app.controller("con",function($scope) {

$scope.class = "red";
$scope.changeClass = function(){
if ($scope.class === "red")
  $scope.class = "blue";
else
  $scope.class = "red";
};

});

.red{
 color:red;
 }

  .blue{
  color:blue;
 }

1 Comment

<script src="ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"> </script> <body ng-app="ap" ng-controller="con"> <div ng-class="class">{{class}}</div> <button ng-click="changeClass()">Change Class</button> </body>

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.