📖 Scenario: You are building a simple Angular component to display a user's profile picture. The image source URL will be set dynamically using property binding.
🎯 Goal: Create an Angular standalone component that uses property binding with square brackets to set the src attribute of an <img> tag dynamically from a component variable.
📋 What You'll Learn
Create a standalone Angular component named UserProfileComponent.
Define a string variable profileImageUrl in the component class with the exact value 'https://example.com/user.png'.
Use property binding with square brackets to bind the src attribute of an <img> element to profileImageUrl.
Add an alt attribute with the value 'User profile picture' for accessibility.
Ensure the component is standalone and imports CommonModule.
💡 Why This Matters
🌍 Real World
Property binding is commonly used in Angular to dynamically set element properties like image sources, form inputs, and more, based on component data.
💼 Career
Understanding property binding is essential for Angular developers to build interactive and dynamic web applications that respond to data changes.
Progress0 / 4 steps
1
Create the Angular component with the image URL variable
Create a standalone Angular component named UserProfileComponent. Inside the component class, define a string variable called profileImageUrl and set it to 'https://example.com/user.png'.
Angular
Hint
Use @Component decorator with standalone: true. Define profileImageUrl inside the class.
2
Add the image element with property binding for src
In the template property of UserProfileComponent, add an <img> element. Use property binding with square brackets to bind the src attribute to the profileImageUrl variable.
Angular
Hint
Use [src]="profileImageUrl" inside the <img> tag in the template string.
3
Add alt attribute for accessibility
Add an alt attribute to the <img> element in the template with the exact value 'User profile picture' to improve accessibility.
Angular
Hint
Inside the <img> tag, add alt="User profile picture".
4
Verify standalone component imports CommonModule
Ensure the UserProfileComponent is standalone and imports CommonModule in the imports array of the @Component decorator.
Angular
Hint
Check the @Component decorator has standalone: true and imports: [CommonModule].
Practice
(1/5)
1. What does property binding with square brackets [] do in Angular?
easy
A. It creates a two-way data binding between component and template.
B. It binds a component property to an element property, updating the UI automatically.
C. It binds an event handler to an element event.
D. It defines a CSS class for an element.
Solution
Step 1: Understand property binding purpose
Property binding connects component data to element properties, so UI updates when data changes.
Step 2: Differentiate from other bindings
Event binding uses parentheses, two-way binding uses both brackets and parentheses, CSS class binding uses special syntax.
Final Answer:
It binds a component property to an element property, updating the UI automatically. -> Option B
Quick Check:
Property binding = UI updates from component data [OK]
Hint: Square brackets mean binding to element property [OK]
Common Mistakes:
Confusing property binding with event binding
Thinking it creates two-way binding
Using it to bind CSS classes incorrectly
2. Which of the following is the correct syntax for binding the src property of an <img> tag to a component variable imageUrl?
easy
A.
B.
C.
D.
Solution
Step 1: Identify property binding syntax
Property binding uses square brackets around the property name, like [src].
Step 2: Check correct attribute usage
Inside quotes, use the component variable name without extra brackets or parentheses.
Final Answer:
<img [src]="imageUrl"> -> Option C
Quick Check:
Square brackets + variable name = correct property binding [OK]
Hint: Use [property]="variable" for property binding [OK]
Common Mistakes:
Using interpolation {{}} inside src attribute for property binding
Using parentheses instead of brackets for property binding
Thinking brackets can't be used on input
5. You want to disable a button dynamically based on a component boolean variable isLoading. Which Angular template code correctly disables the button when isLoading is true and enables it otherwise?
hard
A. <button [disabled]="isLoading">Submit</button>
B. <button disabled="isLoading">Submit</button>
C. <button [disabled]="!isLoading">Submit</button>
D. <button (disabled)="isLoading">Submit</button>
Solution
Step 1: Understand property binding for boolean attributes
Use square brackets to bind the disabled property to the component variable.
Step 2: Check logic for disabling
When isLoading is true, disabled should be true to disable the button.
Step 3: Evaluate options
<button [disabled]="isLoading">Submit</button> binds disabled to isLoading correctly. <button disabled="isLoading">Submit</button> treats isLoading as string, not variable. <button [disabled]="!isLoading">Submit</button> disables when isLoading is false (wrong logic). <button (disabled)="isLoading">Submit</button> uses parentheses which is event binding, invalid here.
Final Answer:
<button [disabled]="isLoading">Submit</button> -> Option A
Quick Check:
Bind [disabled] to boolean variable directly [OK]
Hint: Bind [disabled] to boolean variable for dynamic enable/disable [OK]
Common Mistakes:
Using disabled="isLoading" treats it as string
Negating isLoading incorrectly disables button
Using parentheses () instead of brackets []
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.