In Angular, sometimes when using *ngIf
, you have to check a bunch of conditions whether to render the element or not. Sometimes it becomes a bit harder to read. Is using getter methods for cases like this a bad idea? Why or why not?
Should they be completely avoided?
I have tried storing the conditions in a variable before for readability but I encountered problems (I was checking the values of the properties of the class), I'm not sure but I think it had something to do with outdated data, they're not returning the correct result but when I used getters, the DOM gets updated when something changes, basically I get the correct results.
Something like:
In the view:
<div [ngClass]="{ 'hidden' : isFlagUnchecked}"></div>
In the component class:
get isFlagUnchecked(): boolean { return !this.profileForm.get('flag').value; }
OR: (An example of grouped conditions, basically looking for ways so they can be further simplified or encapsulated with a more descriptive name, like storing in a single variable, or if they should be)
<div *ngIf="!data['content_id'] || !data['content_name']" class="container"></div>