📖 Scenario: You are building a simple Angular component that shows a welcome message only if the user is logged in. This is a common pattern in websites where some content is visible only to logged-in users.
🎯 Goal: Create an Angular standalone component that uses *ngIf to conditionally show a welcome message when the user is logged in.
📋 What You'll Learn
Create a boolean variable isLoggedIn to represent the user's login status.
Add a configuration variable welcomeMessage with the text to display.
Use *ngIf in the template to show the welcome message only if isLoggedIn is true.
Complete the component with proper Angular standalone setup and accessibility.
💡 Why This Matters
🌍 Real World
Conditional rendering is essential in web apps to show or hide content based on user actions or status, like login state or permissions.
💼 Career
Understanding *ngIf and conditional rendering is fundamental for Angular developers building interactive and user-friendly applications.
Progress0 / 4 steps
1
Create the login status variable
In the Angular component class, create a boolean variable called isLoggedIn and set it to false.
Angular
Hint
Use isLoggedIn = false; inside the component class.
2
Add the welcome message variable
Add a string variable called welcomeMessage in the component class and set it to 'Welcome back, user!'.
Angular
Hint
Use welcomeMessage = 'Welcome back, user!'; inside the component class.
3
Use *ngIf to conditionally show the message
In the component's template, use *ngIf="isLoggedIn" on a <p> tag to display the welcomeMessage only when isLoggedIn is true.
Angular
Hint
Use <p *ngIf="isLoggedIn">{{ welcomeMessage }}</p> in the template.
4
Complete the component with accessibility and toggle
Add a button below the message with (click) event to toggle isLoggedIn. Add aria-live="polite" to the <p> tag for accessibility.
Angular
Hint
Add aria-live="polite" to the <p> tag and a button with (click)="toggleLogin()". Define toggleLogin() method to flip isLoggedIn.
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.