0

I'm looping over an array that contains a varying amount of percentages. However, I would like to add classes based on if an answer from the database equals one of the values from the array.

The below example loops over the values in the array. What i'm trying to do is say, 'if the answer is '0', apply a class with the name 'red', if not, add a class of 'transparent'. If the answer happens to be '20', add a class name of 'amber'. If not, 'transparent'.

I seem to be hitting a wall with the multiple variations that i'm trying.

<div *ngFor="let percent of percentages" 
     [ngClass]="{
         (answer =='0') ? 'red':'transparent',
         (answer =='20') ? 'amber':'transparent',         
     }">
     {{ percent }}
</div>

EDIT - For clarification What I'm attempting to do is simplify the below. This is writing each of the percentages as it's own div. What I'm attempting to do is achieve the same result but by looping through an array of percentages instead.

<div [ngClass]="answer==='0' ? 'red' : 'bg-transparent'"> 0% </div>

<div [ngClass]="answer==='25' ? 'amber' : 'bg-transparent'"> 25% </div>

<div [ngClass]="answer==='50' ? 'orange' : 'transparent'"> 50% </div>

2
  • Not everything has to go inside the template. Just create a function for it if it makes life easier for you. :) Commented May 12, 2020 at 13:28
  • but in your question answer is general word. so it effect all. so u need add one more property to each element of percentages Commented May 12, 2020 at 13:49

4 Answers 4

3

put other condition to else option part demo

 <div *ngFor="let percent of percentages" 
         [ngClass]="(answer =='0') ? 'red': (answer =='20') ? 'amber':'transparent ">
         {{ percent }}
    </div>
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for the time! - However, if the answer is 0, it's making all divs come back red, opposed to the single one that has an answer of 0.
Okey is your condition if 0 then red if 20 then amber else transparent ?
I've updated the question in hope of providing more clarity
but in your question answer is general word. so it effect all. so u need add one more property to each element of percentages
'answer' is the value from the database. It feels like something like this, but multiple versions should work: answer == percent && answer == '0': 'red'. Just not sure how to do multiple conditions for the other percentages. Do you think that'll work?
|
0

You can use Ternery operator

<div *ngFor="let percent of percentages"
 [ngClass]="{ answer == '0' ? 'red' : answer == '20' ? 'amber' : 'transparent'}">
 {{ percent }}
</div>

Comments

0

How about moving your condition in your component rather than in template?

Something like this

// template
<div *ngFor="let percent of percentages" 
     [ngClass]="foo(answer)">
     {{ percent }}
</div>


foo(answer) {
   if(answer === '0') {
      return 'red';
   }
   if (answer =='20' ) { 
      return 'amber'
   }    

// add more condition here or use a switch statement above.
    return 'transparent';
}

7 Comments

Thanks for the response! - I've updated the question for more clarity. At the moment this will apply a class of 'red' to all divs within the ngFor. When in fact, it should only be the div that has an answer of 0. Also, if my array had 20 percentage options, this function would be called 20 times?
@Haven even without a function, you will run your condition 20 times ;) that's why I used return right away so that the rest of the code won't run
not sure what you meant by this will apply a class of red to all divs. you can see that they'll only get a class of red when the answer is 0
I think i'm finding it hard to articulate the requirement. If the answer is 0, only the div that has a percentage of 0 should have a red background. All others shouldn't. Does that make sense?
@Haven Do you mean something like this by any chance? stackblitz.com/edit/…
|
0

there're a syntax for ngClass that is {'class1':condition1,'class2':condition2...} So I think that is more indicated

<div class="transparent" [ngClass]="{'red':answer == '0',
                                     'amber':answer == '20',
                                     'orange':answer==50}">

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.