1

I have a list of li items with functions attached to them. I also have an event listener which attaches a class of "current" to whichever li item is being clicked.

HTML

<ul class="cf" id="filter">
 <li ng-click="getRadius(5)" class="current"><a href="#">5 km</a></li>
 <li ng-click="getRadius(10)"><a href="#">10 km</a></li>
 <li ng-click="getRadius(25)"><a href="#">25 km</a></li>
 <li ng-click="getRadius(50)"><a href="#">50 km</a></li>
 <li ng-click="getRadius(100)"><a href="#">100 km</a></li>
</ul>

Is there a way to disable the ng-click event if that specific li item has a class of "current" or is there a better way to go about this?

4
  • When you assign the .current class also set a property on the model and bind on that...? Commented Jul 31, 2015 at 11:17
  • I want getRadius functionality??? Commented Jul 31, 2015 at 11:21
  • @Petrichor Could you give me an example of that? I'm still new to Angular. Commented Jul 31, 2015 at 11:24
  • 1
    Use a css property pointer-events: none Commented May 20, 2016 at 17:45

4 Answers 4

1

You cannot disable a list cause its not a interactive element can use ngClass to apply a specific class when disabled to make it appear disabled:

<li ng-class="{'disabled':condition}"ng-click="getRadius(5)">item</li>

You can use ng-if to remove those items completely from the list:

 <li ng-if="!condition" ng-click="getRadius(5)">item</li>
 <li ng-if="condition" >item</li>
Sign up to request clarification or add additional context in comments.

Comments

0

If you use button tag as trigger instead of li, you can use ng-disabled directive to permit click by a condition. For example:

<ul class="cf" id="filter">
 <li><button ng-click="getRadius(5)" ng-disabled="current!=5">5 km</button></li>
 <li><button ng-click="getRadius(10)" ng-disabled="current!=10">10 km</button></li>
 <li><button ng-click="getRadius(25)" ng-disabled="current!=25">25 km</button></li>
 <li><button ng-click="getRadius(50)" ng-disabled="current!=50">50 km</button></li>
 <li><button ng-click="getRadius(100)" ng-disabled="current!=100">100 km</button></li>
</ul>

If you want, you can customize button style and you can show it like a standart text element.

.cf button {
  border: none;
  background: none;
}

Comments

0
// template
<ul class="cf" id="filter">
 <li ng-click="!clickLi[5] && getRadius(5)" class="current"><a href="#">5 km</a></li>
 <li ng-click="!clickLi[10] && getRadius(10)"><a href="#">10 km</a></li>
 <li ng-click="!clickLi[25] && getRadius(25)"><a href="#">25 km</a></li>
 <li ng-click="!clickLi[50] && getRadius(50)"><a href="#">50 km</a></li>
 <li ng-click="!clickLi[100] && getRadius(100)"><a href="#">100 km</a></li>
</ul>    


// controller
function MyController($scope) {
    $scope.clickLi = {
        5: true
    };

    $scope.getRadius = function(li_id) {
        $scope.clickLi[li_id] = true;
        console.log(li_id);
    };
}

A demo on JSFiddle.

Comments

0

Possible workaround: If you need a single selection you can add variable into the scope specifying which row is selected and generate your list with the ng-repeat, then you can add lazy checking on ng-click if current $index is equal to selected index, you can use the same condition to apply current class with ng-class.

For example:

app.js

$scope.selected = 0;
$scope.distances = [5, 10, 25, 50, 100];  

app.html

<ul class="cf" id="filter">
  <li ng-repeat = "distance in distances" ng-click="($index == selected) || getRadius(distance)" ng-class="{'current':($index == selected)}"><a href="#">{{distance}} km</a></li>
</ul>

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.