Skip to main content

angular - How to toggle element visibility

app.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})


export class AppComponent {
  isVisible:boolean = true;

  toggleVisibility(){
    this.isVisible = !this.isVisible;
  }
}
app.component.html

<h2>Angular - Toggle Element Visibility</h2>

<button 
    (click)="toggleVisibility()"
    style="padding:12px;">
        Toggle Visibility
</button>

<h3 *ngIf="isVisible">
    Show Hide Me By Button Click Event
</h3>
More Angular tutorials