Showing and hiding in Angular
We will walk through examples of showing and hiding components or elements in Angular.
Showing and hiding in Angular
While developing business applications we need to hide some data based on user roles or conditions. We have to display the same data based on the conditions in this application.
In this tutorial, I will show you a simple method using which we can show and hide data based on conditions.
Let us create a new application using the following command.
# angular
ng new my-app
After creating our new application in Angular, we will go to our application directory using this command.
# angular
cd my-app
Now, let's run our application to check if all dependencies are installed correctly.
# angular
ng serve --open
Suppose we have a template or a piece of code that we want to show only when a condition is true and hide it when it is false. We can show and hide it using this example.
First, we'll app.component.html
create a demo content with a title, paragraph, and input field in and wrap it in div
.
# Angular
<div>
<hello name="{{ name }}"></hello>
<p>Enter your name:</p>
<input type="text" id="element"/>
</div>
Now, app.component.ts
in , we create a new variable element
and set it to false
.
# Angular
element = false;
We will use *ngIf
show and hide ours based on the condition div
.
# Angular
<div *ngIf="element == true">
<hello name="{{ name }}"></hello>
<p>Enter your name:</p>
<input type="text" id="element"/>
</div>
As shown in the example above, we set a condition that if element
is true
, then it will be displayed div
, and if the condition is false
, then it will not be displayed.
Therefore, div
will be hidden because we have element
set to false
. Now, let's element
change the value of to true
and check how it works.
Output:
You can see now that when we change element
the value of , the condition we div
set on changes and it starts showing div
and all the elements inside it.
Imagine if we wanted to show and hide data when certain buttons are clicked. We can *ngIf
create this simple example using the same .
Now, let's create two buttons with two functions showData()
and . We need to create these two buttons outside of so that they are not hidden when the condition changes to .hideData()
div
false
Therefore, our app.component.html
code in will look like this.
# Angular
<div *ngIf="element == true">
<hello name="{{ name }}"></hello>
<p>Enter your name:</p>
<input type="text" id="element" />
</div>
<button (click)="showData()">Display Data</button>
<button (click)="hideData()">Hide Data</button>
Now, let's app.component.ts
create these two functions in . showData()
Change element
the value of to true
and change the value of hideData()
to .element
false
So the code will look like this.
# Angular
showData() {
return (this.element = true);
}
hideData() {
return (this.element = false);
}
We will now add some CSS code to make our button look nice.
# Angular
button{
margin-top: 10px;
padding: 10px;
background-color: aquamarine;
border: none;
margin-right: 10px;
}
Let’s see how it works.
Output:
As you can see in the examples above, we can easily hide and show content using *ngIf
the and button methods.(click)
We can use the same concept on buttons as well
if we want to hide the button when the content is hidden Display Data
and hide the button when the data is hidden .Hide Data
So we create a *ngIf
condition, if element
is true
, then only the Hide Data button will be displayed, and when element
is false
, then the Show Data button will be displayed.
# Angular
<div *ngIf="element == true">
<hello name="{{ name }}"></hello>
<p>Enter your name:</p>
<input type="text" id="element" />
</div>
<button *ngIf="element == false" (click)="showData()">Display Data</button>
<button *ngIf="element == true" (click)="hideData()">Hide Data</button>
We don't need app.component.ts
to write any line of code in the ./config file because the functions we created and *ngIf
the ./config function will help us to do the same thing on our buttons. So, let's check the result of how it works now.
Output:
So, in this tutorial, we have learned how to use to *ngIf
hide and show content based on conditions, and we have also learned how to use (click)
the method of a button to help us hide and show data on a button click.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Setting default option value for Select From Typescript in AngularJs
Publish Date:2025/04/18 Views:185 Category:Angular
-
select Is an HTML tag that contains n an option subtag that contains a value attribute. This tutorial guide will provide how to select default option value from TypeScript in AngularJs if the user does not select any specific defined value.
Enabling HTML5 mode in AngularJS
Publish Date:2025/04/18 Views:148 Category:Angular
-
This article will guide you through enabling HTML5 mode with deep linking on your AngularJS application. Using HTML5 mode in AngularJS $locationProvider.html5Mode pushState is a way to tell the browser that it needs to use HTML5 mode for UR
Loading spinner in AngularJs
Publish Date:2025/04/18 Views:133 Category:Angular
-
We will cover how to add a loading spinner while the request is loading and stop the loader while data is loading in AngularJs. Loading spinner in AngularJs Loaders are a part of web applications to make them user-friendly and improve the u
Select an option in the Angular drop-down menu
Publish Date:2025/04/18 Views:112 Category:Angular
-
We will cover how to set the selected option in an Angular dropdown, set the selected option from an array of options, and set the selected option if the options are dynamic in Angular. Setting the selected option in an Angular dropdown men
在 Angular 下拉菜单中选择选项
Publish Date:2024/03/24 Views:210 Category:Angular
-
本教程演示了如何在 Angular 下拉菜单中设置选定的选项。
在 Angular 中上传文件
Publish Date:2023/04/14 Views:123 Category:Angular
-
本教程演示了如何在 Angular 中上传任何文件。我们还将介绍如何在文件上传时显示进度条,并在上传完成时显示文件上传完成消息。
Angular 中所有 Mat 图标的列表
Publish Date:2023/04/14 Views:189 Category:Angular
-
本教程演示了在哪里可以找到 Angular 中所有 Mat 图标的列表以及如何使用它们。
Angular 2 中的复选框双向数据绑定
Publish Date:2023/04/14 Views:194 Category:Angular
-
本教程演示了如何一键标记两个复选框。这篇有 Angular 的文章将着眼于执行复选框双向数据绑定的不同方法。
在 AngularJS 中重新加载页面
Publish Date:2023/04/14 Views:205 Category:Angular
-
我们可以借助 windows.location.reload 和 reload 方法在 AngularJS 中重新加载页面。