Home > database >  function inside template no work but when set logic inside template work? Angular 9
function inside template no work but when set logic inside template work? Angular 9

Time:11-09

I have logic which work good -> example:

<div *ngIf="pagination && pagination.tp >= 1">
 ...
</div>

This is work good. When I want to set my values inside ts and switch logic in ts.file component:

<div  *ngIf="isPagination">
 ...
</div>

and inside ts component:

  public isPagination() { 
     return this.pagination && this.pagination?.tp >= 1 ? false : true; 
  } 

No work.

WHy ?

I am also try with:

<div  *ngIf="isPagination()"> add only ()  isPagination()
 ...
</div>

No work

What is problem ?

CodePudding user response:

Because your isPagination method returns the exact opposite of what you expect. There's no need for the ternary operator part (condition1 && condition2 ? true : false) - the statement will be coerced into a boolean anyway.

This is the correct function:

public isPagination() { 
  return this.pagination && this.pagination?.totalPages >= 1;
} 

CodePudding user response:

Your isPagination method should be rearrange as suggested by @tsvetan-ganev. Other than that you must invoke isPagination method with Parenthesis.

<div *ngIf="isPagination()">
</div>
  • Related