*ngIf helps you show or hide parts of your webpage based on conditions. It makes your app smarter by only displaying what is needed.
Show a login button only when the user is not logged in.
Display a loading spinner while data is being fetched.
Hide a form after it is submitted successfully.
Show error messages only when there is an error.
Syntax
Angular
<div *ngIf="condition">Content to show iftrue</div>
The *ngIf directive uses a condition inside quotes.
If the condition is true, Angular shows the element; if false, it removes it from the page.
Examples
Shows the welcome message only if isLoggedIn is true.
Angular
<p *ngIf="isLoggedIn">Welcome back!</p>
Shows the login button only if isLoggedIn is false.
Angular
<button *ngIf="!isLoggedIn">Login</button>
Shows the message only if the items array is not empty.
Angular
<div *ngIf="items.length > 0">You have items in your list.</div>
Sample Program
This component shows a welcome message and a button to toggle login status. It uses *ngIf to show different messages depending on whether isLoggedIn is true or false.
Angular
import { Component } from'@angular/core';
@Component({
selector: 'app-root',
template: `
<h1>Welcome to the app!</h1>
<button (click)="toggleLogin()">Toggle Login</button>
<p *ngIf="isLoggedIn">You are logged in.</p>
<p *ngIf="!isLoggedIn">Please log in to continue.</p>
`
})
exportclass AppComponent {
isLoggedIn = false;
toggleLogin() {
this.isLoggedIn = !this.isLoggedIn;
}
}
OutputSuccess
Important Notes
Using *ngIf removes or adds elements from the DOM, which can improve performance.
Remember to use parentheses for event binding like (click) to change conditions.
Summary
*ngIf shows or hides elements based on a condition.
It helps create dynamic and responsive user interfaces.
Use it to control what users see depending on app state.
Practice
(1/5)
1. What does the Angular directive *ngIf do in a template?
easy
A. It loops over a list and renders each item.
B. It styles an element dynamically.
C. It binds a variable to an input field.
D. It conditionally includes or removes an element from the DOM based on a boolean expression.
Solution
Step 1: Understand the purpose of *ngIf
*ngIf is used to conditionally render elements in Angular templates.
Step 2: Identify what *ngIf controls
It controls whether an element is included or removed from the DOM based on a true or false condition.
Final Answer:
It conditionally includes or removes an element from the DOM based on a boolean expression. -> Option D
Quick Check:
*ngIf = conditional rendering [OK]
Hint: Remember *ngIf shows or hides elements by condition [OK]
Common Mistakes:
Confusing *ngIf with *ngFor for looping
Thinking *ngIf changes styles only
Assuming *ngIf binds data to inputs
2. Which of the following is the correct syntax to conditionally display a paragraph only if isVisible is true?
easy
A. <p *ngIf="isVisible">Hello</p>
B. <p ngIf="isVisible">Hello</p>
C. <p *ngFor="isVisible">Hello</p>
D. <p *ngShow="isVisible">Hello</p>
Solution
Step 1: Identify the correct Angular directive syntax
Angular uses *ngIf with an asterisk to indicate a structural directive.
Step 2: Check the attribute and expression format
The expression should be inside quotes after *ngIf=, like *ngIf="isVisible".
Final Answer:
<p *ngIf="isVisible">Hello</p> -> Option A
Quick Check:
Use *ngIf with asterisk and quotes [OK]
Hint: Look for *ngIf with asterisk and quotes [OK]
But the app crashes with an error: Cannot read property 'isLoggedIn' of undefined. What is the best fix?
medium
A. Remove the *ngIf directive completely.
B. Change to *ngIf="user?.isLoggedIn" to safely check property.
C. Initialize user as an empty object in the component.
D. Use *ngIf="user.isLoggedIn === true" instead.
Solution
Step 1: Identify cause of error
The error occurs because user is undefined when Angular tries to read user.isLoggedIn.
Step 2: Use safe navigation operator
Using user?.isLoggedIn prevents error by checking if user exists before accessing isLoggedIn.
Final Answer:
Change to *ngIf="user?.isLoggedIn" to safely check property. -> Option B
Quick Check:
Use ? to avoid errors on undefined objects [OK]
Hint: Use ?. to safely access nested properties [OK]
Common Mistakes:
Removing *ngIf loses conditional rendering
Initializing user empty but not fixing undefined timing
Checking equality without safe navigation still errors
5. You want to display a message only if a user has a non-empty email property and is verified (isVerified true). Which *ngIf expression correctly handles this?
hard
A. *ngIf="user.email == '' && user.isVerified"
B. *ngIf="user.email || user.isVerified"
C. *ngIf="user.email && user.isVerified"
D. *ngIf="user.email.length > 0 || user.isVerified"
Solution
Step 1: Understand the condition requirements
The message should show only if user.email is not empty (truthy) AND user.isVerified is true.
Step 2: Analyze each option
*ngIf="user.email && user.isVerified" uses logical AND to require both conditions true. *ngIf="user.email || user.isVerified" uses OR, which is incorrect. *ngIf="user.email == '' && user.isVerified" checks for empty string which is opposite of requirement. *ngIf="user.email.length > 0 || user.isVerified" uses OR and length check but allows message if only one condition is true.
Final Answer:
*ngIf="user.email && user.isVerified" -> Option C
Quick Check:
Use AND (&&) to require both conditions true [OK]
Hint: Use && to require both conditions true [OK]
Common Mistakes:
Using || instead of && allowing partial matches
Checking for empty string instead of truthy value
Using length without checking for undefined user
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.