1

any way to make an expression for ngClass to be a conditional. For example, I have tried the following using javascript:

 $scope.setEnabled = function(status){
    $scope.filterEnabled = status;
    if(status){
      angular.element(document.querySelector( '#enabledFalse')).removeClass('active-btn');
      angular.element(document.querySelector( '#enabledTrue')).addClass('active-btn');
    } else {
      angular.element(document.querySelector( '#enabledTrue')).removeClass('active-btn');
      angular.element(document.querySelector( '#enabledFalse')).addClass('active-btn');
    }  
  }

And view :

div class="buttons"> 
        <a id="enabledFalse" href="" ng-click="setEnabled(0)" class="click-btn active-btn">No</a> 
        <a id="enabledTrue" href="" ng-click="setEnabled(1)" class="click-btn">Yes</a> 
      </div>

What's the best way to apply this using ngClass?

2 Answers 2

2

Here is what the conditional statement would look like:

<a href class="click-btn" ng-class="{ 'active-btn' : filterEnabled }" ng-click="setEnabled(0)">No</a>
<a href class="click-btn" ng-class="{ 'active-btn' : !filterEnabled }" ng-click="setEnabled(1)">Yes</a>

And the js would simply be:

$scope.setEnabled = function(status){
  $scope.filterEnabled = status;
}

Edit: As an alternative, here's the most simplified method I can think of:

<a href class="click-btn" ng-class="{ 'active-btn' : filterEnabled }" ng-click="filterEnabled = !filterEnabled">{{ filterEnabled ? 'Yes' : 'No' }}</a>

You won't need anything in the js file; the Yes/No text and active-btn class will toggle when you click this single button, because they depend solely on the value of filterEnabled.
This is assuming only having one button is acceptable.

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

Comments

0

Outside of my SO-tag comfort zone, here, but you can use a ternary expression, where each tag is setup as the negation of the other:

... id="enabledFalse" ng-class="( filterEnabled)? 'active-btn' : ''"
... id="enabledTrue"  ng-class="(!filterEnabled)? 'active-btn' : ''"

Other classes to be applied unconditionally can be placed in a regular class attribute.

Also in each tag:

ng-click="filterEnabled=!filterEnabled"

if you want each to toggle, or call the function as you have it.

5 Comments

But how I can change status using this ?
Status is depent link : ng-click="setEnabled(0)"
Do you want clicking to toggle the value? See edit, The variable that you've put into scope is called filterEnabled. status is just a param name (local) in the setEnabled function.
but what about remove class : angular.element(document.querySelector( '#enabledFalse')).removeClass('active-btn'); angular.element(document.querySelector( '#enabledTrue')).addClass('active-btn');
You don't add or remove. In angular its done declaratively. The ng-class expression gets evaluated and the class (or classes) that it evaluates to gets applied.

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.