Search code examples
angulartypescriptangular-ng-if

Showing a div based on button click


I'm trying to show a div based on whether button is clicked or not, however this doesn't seem to be working.

Component HTML

<button ng-click="showFormToggle()">Click</button>

<div ng-show="showForm">

</div>

I've also tried

<button (click)="showFormToggle()">Click</button>

<div *ngIf="showForm">

</div>

Component Typescript

 showForm: boolean = false;

 showFormToggle(){
    this.showForm = true;
    console.log(this.showForm);
  }

Solution

  • Based on your Component HTML, you may be confused as the version of Angular you are using. I am assuming you are intending to use Angular2+, considering your naming convention (Component vs Controller), and therefore the first example no longer works (That is AngularJS). component.ts

    showForm : boolean = false;
    

    component.html

    <button (click)="showForm = !showForm">Click</button>
    <div *ngIf="showForm">
    </div>
    

    or

    <button (click)="showForm = !showForm">Click</button>
    <div [hidden]="!showForm">
    </div>
    

    Please confirm what you are using, Angular 2+ or AngularJS.