How to hide an HTML element via a button click in AngularJS ?
Last Updated :
01 Aug, 2021
In this article, we will see how to use a toggle button feature to hide and display an element by button click in Angular. The toggle button is a user interface control that may be useful for situations when the user wants to switch between 2 states or conditions. For instance, in our smartphone, we usually turn off/on the button to wireless connected devices such as Bluetooth, Wifi, Airplane mode, etc. This kind of toggle button makes things easier to use in everyday life. This article will show how to make a toggle data button using Angular JS.
ngif Directive:The ngIf is an Angular structural directive that creates a template based on the value of the expression evaluation. The value of the expression should always be either True or False. When the expression inside the template evaluates to be true, then the block of code inside the then block is rendered and whereas the expression evaluates to be false, the else block, which is optional, gets rendered.
Syntax:
<div *ngIf = "condition">Content to render when condition is true.</div>
For the app.component.html file, we will use this toggleData() function to call the function in the Html
<button (click)="toggleData()">Toggle Data</button>
For the app.component.ts file, we have defined a function that toggles the button to display or hide the content.
export class AppComponent {
toDisplay = true;
toggleData() {
this.toDisplay = !this.toDisplay;
}
}
Approach:
- First, wrap the content inside a <div> tag in an Html file.
- Now take a variable "toDisplay" and bind it to the <div> tag.
- In an HTML file, include a button and attach a function call to it whenever the user clicks on it.
- In the ts file, implement the function and now toggle the toDisplay variable between true and false.
- Once the above implementation is done, start the application
Project Structure: This will look like the following image
Example:
app.component.html
<div *ngIf="toDisplay">
<p>Click the below button to display and hide the data</p>
</div>
<button (click)="toggleData()">Toggle Data</button>
app.component.ts
import { Component } from "@angular/core";
@Component({
selector: "my-app",
templateUrl: "./app.component.html",
})
export class AppComponent {
toDisplay = true;
toggleData() {
this.toDisplay = !this.toDisplay;
}
}
Output: From the above output, we can see by clicking the Toggle Data button, we can able to display or hide the data.
Similar Reads
How to disable a button depending on a checkboxâs state in AngularJS ? In this article, we will learn to disable the button depending upon the check-box status in Angular. We will use the Angular JS directive named ng-disabled to disable the button by unchecking the box. Please refer to AngularJS ng-disabled Directive. The ng-disabled directive is used to enable or dis
2 min read
How to Create Button Dynamically with Click Event in Angular ? The task is to create a button dynamically with a click event using AngularJS, i.e., whenever someone clicks on the button then a new button gets created. The easiest way to create a button in AngularJS is to use the ng-repeat directive. We can easily hook up a repeat logic inside a button click eve
2 min read
How to hide block of HTML code on a button click using jQuery ? In this article, we will learn how to hide a block of HTML code with a button click. We can do this by using the jQuery inbuilt hide() method. Let's briefly learn the functionality of this method. hide(): In CSS, we have a property display:none which basically hides the element. This hide() method i
2 min read
How to Remove HTML element from DOM using AngularJS ? In this article, we will see how to remove particular HTML elements from the DOM with the help of AngularJS, along with understanding its implementation through the examples. For this, we can use the remove() method that helps to remove all the selected elements including all the text. Syntax: selec
2 min read
How to empty the content of an element using AngularJS ? In this article, we will see how to remove the content of an element in HTML DOM using AngularJS. This task can be accomplished by implementing the jQuery empty() method that removes all child nodes and their content for the selected elements. Syntax: element.empty();Parameter: This method does not
2 min read
How to get original element from ng-click in AngularJS ? In AngularJS, we can get the original DOM element that fired the ng-click event using the event's currentTarget property. Getting the event's currentTarget can be useful when we want to track an element's activity, in this case, whether and how many times a particular element is clicked, or we want
3 min read