1

I am currently refactoring some old code and thus dealing with many redundant conditional checks. Here is a short example:

<div *ngIf="user" title="{{user?.name}}">
    <span>Age: {{user?.age}}<span>
</div>

Most entries like 'user age', are a no-brainer, because we already know that the user exists. There is a clear hierarchy in the usage of encapsulated HTML tags.

I am concerned with the order within the tag. Can i safely remove the conditional check in 'user name'?

connectedMind
  • 421
  • 4
  • 17
  • even if user is defined, user.age might not be. I usually write something like `{{user?.age || '-'}}` so it displays a dash (rather than nothing) if value is empty – O-9 Dec 15 '22 at 14:58
  • Yes, you can remove them since your ngIf won't render the DIV into the DOM if user is null or undefined. – Sean Chase Dec 15 '22 at 15:18

0 Answers0