Property binding lets you set HTML element properties from your component data. It keeps your page dynamic and interactive without extra code.
Property binding with square brackets in Angular
<element [property]="componentValue"></element>[] around the property name to bind it to a component variable or expression.src property of the image to the imageUrl variable from the component.<img [src]="imageUrl">isDisabled is true in the component.<button [disabled]="isDisabled">Click me</button>userName from the component.<input [value]="userName">This component shows a greeting with the user name. The input box uses property binding to set its value from userName. When you type, it updates userName. The submit button is disabled if userName is empty.
It uses property binding with square brackets to keep the input and button properties in sync with the component data.
import { Component } from '@angular/core'; @Component({ selector: 'app-root', template: ` <h1>Welcome, {{ userName }}!</h1> <input [value]="userName" (input)="userName = $event.target.value" aria-label="User name input"> <button [disabled]="!userName">Submit</button> ` }) export class AppComponent { userName = 'Alice'; }
Property binding updates the DOM property, not the HTML attribute. This is important for elements like input where the property controls the displayed value.
Always use property binding for dynamic values instead of string interpolation inside attributes to avoid unexpected behavior.
Use aria-label or other accessibility attributes to keep your UI accessible.
Property binding with square brackets connects component data to element properties.
It keeps the UI updated automatically when data changes.
Use it for dynamic attributes like src, disabled, and value.
Practice
[] do in Angular?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 BQuick Check:
Property binding = UI updates from component data [OK]
- Confusing property binding with event binding
- Thinking it creates two-way binding
- Using it to bind CSS classes incorrectly
src property of an <img> tag to a component variable imageUrl?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 CQuick Check:
Square brackets + variable name = correct property binding [OK]
- Using interpolation {{}} inside src attribute for property binding
- Using parentheses () which are for event binding
- Putting brackets inside quotes incorrectly
export function MyComponent() {
const [isDisabled, setIsDisabled] = React.useState(true);
return <button [disabled]="isDisabled">Click me</button>;
}What will happen when this Angular template renders?
Solution
Step 1: Identify framework mismatch
The code uses React hooks inside Angular template, which is invalid syntax in Angular.Step 2: Understand Angular template rules
Angular templates do not support React code or hooks; this causes a syntax error.Final Answer:
There will be a syntax error because React code is used in Angular. -> Option AQuick Check:
React code in Angular template = syntax error [OK]
- Assuming React state works in Angular
- Thinking disabled attribute ignores wrong syntax
- Confusing framework syntax rules
<input [value]=username>
Solution
Step 1: Check property binding syntax
Property binding requires the value inside quotes to be a valid expression.Step 2: Identify missing quotes
Here, username is not quoted, so Angular treats it as invalid syntax.Final Answer:
The value should be in quotes: [value]="username". -> Option DQuick Check:
Property binding expression must be quoted [OK]
- Omitting quotes around binding expressions
- Using parentheses instead of brackets for property binding
- Thinking brackets can't be used on input
isLoading. Which Angular template code correctly disables the button when isLoading is true and enables it otherwise?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 AQuick Check:
Bind [disabled] to boolean variable directly [OK]
- Using disabled="isLoading" treats it as string
- Negating isLoading incorrectly disables button
- Using parentheses () instead of brackets []
Ready to prove it?
Pass the test and this pattern moves unknown
