0

I have the following class

export class PracticeQuestionsListAPI {
  'list':UserOverview[];
  'page-info':string;
  constructor(public pageState:string, public usersList:UserOverview[]){
    this['page-state'] = pageState;
    this['users-list'] = usersList;
  }
}

In my Angular's component's HTML, I want to show a button only if page-state is not an empty string. How could I do it?

I have written something like this but it doesn't compile.

<button *ngIf="!this.users['page-state'].isEmpty()" (click)="getNextPage()" id="next-page-button" class="btn content-div__button--blue btn-sm">Next Page</button>

I also tried *ngIf="!(this.questions['page-state'] =="")" but that doesn't compile either

5
  • Have you tried to escape the quotes? :p Commented Jul 15, 2018 at 9:41
  • 2
    Why do you make your own life difficult by choosing variable names containing a dash, and not respecting standard naming conventions? Use pageInfo, not page-info. Use users, not users-list. Use pageState, not page-state. And declare the properties you're using. Then simply use *ngIf="pageState", or if you really want to accept null and undefined, but not the empty string, use *ngIf="pageState !== ''". Commented Jul 15, 2018 at 9:42
  • Reason to chose dash is these variables map to json data Received from server. I have pageInfo variable as well. I will use that and see if it works Commented Jul 15, 2018 at 9:44
  • You can just use like this *ngIf="!this.users['page-state']", It will check empty, null and undefined. Commented Jul 16, 2018 at 6:27
  • thanks. single quotes worked Commented Jul 16, 2018 at 7:32

1 Answer 1

2

this should work fine

<button *ngIf="this.users['page-state'].length>0" (click)="getNextPage()" id="next-page-button" class="btn content-div__button--blue btn-sm">Next Page</button>

or this

<button *ngIf="this.users['page-state'].length>!==''" (click)="getNextPage()" id="next-page-button" class="btn content-div__button--blue btn-sm">Next Page</button>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. *ngIf="this.questions['page-state']!='' " also worked (inner quotes after != are two single quotes, together they look like a double quote!).
glad i could help

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.