0

I have an HTML table and its rows are being generated by an ngFor loop. I need to add a class to these table cells if some criteria is met.

The issue is that that I need to check if the value of a property is empty AND see if the property even if the property even exists in the first place.

<tbody>
 <tr *ngFor="let e of (modalData.compareData | filter: filterString)">
  <td [class.success]="e.NewRuleVersionID === ''">{{ (e.OldRuleVersionID ? e.OldRuleVersionID  : 'New') }}</td>
  <td [class.success]="e.NewRuleVersionID === ''">{{ (e.OldOutcome ? e.OldOutcome : 'New') }}</td>
  <td [class.danger]="e.OldRuleVersionID === ''">{{ (e.NewRuleVersionID ? e.NewRuleVersionID : 'Removed') }}</td>
  <td [class.danger]="e.OldRuleVersionID === ''">{{ (e.NewOutcome ? e.NewOutcome : 'Removed') }}</td>
 </tr>
</tbody>

In the above code, I am adding a class to those cells if the value doesn't exist. However, there are some records where that property doesn't exist at all. Can this be checked inline within the template so I can account for it?

1
  • Yeah, it should however I am not able to change the structure of the data which is why I was trying to check the property it self for this issue. Commented Oct 17, 2017 at 20:44

1 Answer 1

2

You could check

[class.success]="e?.NewRuleVersionID?.length >= 0"

You could also move template code to Component.

[class.success]="checkProp(e?.NewRuleVersionID)"

Code

checkProp(value){
   return value && value.length > 0;
}
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.