3

In my code, I have a list of cells, and I am displaying that list in the form of buttons. On those buttons, you can see the number of elements inside the cell. If a cell is empty, I want it the button to be green and if it has any elements in it, I want it to be red. My code is below, how can I do this?

 <button mat-button class="green fuse-white-fg" 
                            *ngFor="let prm of cell">Package Count: {{prm.PackageCount}}</button>

3 Answers 3

3

another way to do it as.

<button mat-button [class.green]="!prm.PackageCount" [class.red]="prm.PackageCount" *ngFor="let prm of cell">
  Package Count: {{prm.PackageCount}}
</button>
Sign up to request clarification or add additional context in comments.

Comments

3

You can use ngStyle attribute:

<button mat-button [ngStyle]="{ 'background-color': prm.PackageCount ? 'green' : 'red' }" *ngFor="let prm of cell">
  Package Count: {{prm.PackageCount}}
</button>

Comments

3

You can use ngclass.

 <button mat-button class="fuse-white-fg" 
                    [ngClass] = "{'green':!prm.PackageCount, 'red':prm.PackageCount>0}"
                    *ngFor="let prm of cell">Package Count: {{prm.PackageCount}}</button>

where green and red are css classes

5 Comments

Thanks the buttons turns red know. But, the empty ones (green ones) disappear from the screen. Should I add something else?
how is your green glass defined?
can you tell me if they disappeared from the DOM or are just a color you cannot see?
It's just the color, they haven't disappeared from the DOM
okay, I reread your question and updated my answer accordingly - you want it green if there is nothing, but that doesn't necessarily mean the count is 0

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.