0

I have a list which renders dynamically using a ngFor directive. The data comes from an SQL Lite DB. I am trying to append a class to each list item based on the data present. i.e if the list item is an "Expense" then append a red class, if the list item is an "Income" then append a green class. I have found a similar question here but it only checks for true/false and not exact text.

My code is as follows

<ion-item-sliding *ngFor="let expense of expenses; let i=index">
  <ion-item nopadding [ngStyle]="{greenClass: expense.type==='Income', redClass: expense.type==='Expense'}">
    <p>
      <span>Day of month: {{expense.day}}</span><br>
      Type: {{expense.type}}<br>
      Description: {{expense.description}}
    </p>
    <h3 item-end>
      Amount: R{{expense.amount}}
    </h3>
  </ion-item>
</ion-item-sliding>
2
  • 3
    You should use [ngClass] instead of [ngStyle] Commented Jan 8, 2019 at 7:37
  • oops thanks, my bad Commented Jan 8, 2019 at 7:39

1 Answer 1

3

You are using ngStyle instead of ngClass.

<ion-item-sliding *ngFor="let expense of expenses; let i=index">
  <ion-item nopadding [ngClass]="{greenClass: expense.type==='Income', redClass: expense.type==='Expense'}">
    ...
  </ion-item>
</ion-item-sliding>
Sign up to request clarification or add additional context in comments.

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.