Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

237
Views
Angular 2 conditional class active in second condition not working

I added code to show the sort direction as well as color for selected column see the plunker

  • if sortable show up/down arrow: working
  • if Ascending show down arrow with active color: ACTIVE NOT APPLYING
  • if descending show up arrow with active color: working

Here is the code to application class conditionally:

<i class="fa fa-lg" [ngClass]="{'fa-sort' :  column !== 'ProjectID',  
   ' fa-sort-asc active'  : (column === 'ProjectID' && !isDesc), 
   ' fa-sort-desc active' : (column === 'ProjectID' && isDesc) }" 
    aria-hidden="true"></i>

See ASC and DESC both have active class which is working only with DESC true condition, please see the plunker for more detail.

Here the code after applying all conditions:

<i aria-hidden="true" class="fa fa-lg fa-sort"></i>
<i aria-hidden="true" class="fa fa-lg fa-sort-asc"></i> // active missing
<i aria-hidden="true" class="fa fa-lg fa-sort-desc active"></i>

Any Idea why it is behaving like this:

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Conditions in ngClass are applied in order. When the condition in ngClass is evaluated to false, it removes corresponding classes. If the same class is in two conditions, it might be added at first but in the next condition, it will be removed.

Possible solutions would be:

  1. change style classes to .active-asc, .active-desc {color: red;} and then apply them respectively in the conditions;

or

  1. add new condition 'active' : column === 'columnName'
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!