Property Binding
Learn how Angular uses property binding to connect component data with HTML element properties dynamically.
What is Property Binding?
Property binding is an Angular data binding technique used to set the properties of HTML elements, components, or directives using values from the component.
Property binding uses square brackets
[property] to bind a component value to an
element property.
Property Binding Flow
Component
Component contains the value.
Property
HTML property is selected for binding.
Binding
Angular connects the value with the property.
UI
The element reflects the bound value.
Main Property Binding Concepts
Attribute Properties
Bind values to common HTML element properties.
Disabled State
Dynamically enable or disable an element.
Image Source
Dynamically set an image source using component data.
Class Binding
Dynamically apply CSS classes to elements.
Property Binding Syntax
Property binding uses square brackets around the property name.
<element [property]="expression">
</element>
Angular evaluates the expression on the right side and assigns the result to the specified property.
Binding the Disabled Property
Property binding can dynamically control whether a button is enabled or disabled.
export class StudentComponent {
isDisabled = true;
}
<button [disabled]="isDisabled">
Submit
</button>
Binding the Image Source
An image source can be dynamically assigned using property binding.
export class StudentComponent {
imageUrl = 'assets/images/student.png';
}
<img
[src]="imageUrl"
alt="Student">
Angular reads the value of imageUrl and assigns it
to the image element's src property.
Binding the Input Value
Property binding can set the initial value of an input element.
studentName = 'Sam';
<input
[value]="studentName"
type="text">
Class Property Binding
A CSS class can be dynamically applied using class property binding.
isActive = true;
<div [class.active]="isActive">
Student Profile
</div>
When isActive is true, Angular applies the
active CSS class to the element.
Style Property Binding
Individual CSS properties can also be dynamically bound.
fontSize = '20px';
<p [style.font-size]="fontSize">
Angular Full Stack
</p>
Binding to Component Properties
Property binding can also be used with custom component properties.
studentName = 'Sam';
studentCourse = 'Angular Full Stack';
<app-student
[name]="studentName"
[course]="studentCourse">
</app-student>
Here the parent component sends values to the child component through property binding.
Property Binding Data Flow
Component
isDisabled = true
Binding
[disabled]
Angular
Evaluate
Element
disabled
Interpolation vs Property Binding
| Feature | Interpolation | Property Binding |
|---|---|---|
| Syntax | {{ value }} |
[property]="value" |
| Purpose | Display text | Set element properties |
| Direction | Component → Template | Component → Element |
| Example | {{ studentName }} |
[disabled]="isDisabled" |
| Common Usage | Text content | Element behavior and properties |
Common Uses of Property Binding
Button State
Enable or disable buttons dynamically.
Images
Dynamically assign image source values.
Form Controls
Bind values and states of form elements.
CSS Classes
Dynamically apply classes and styles.
Complete Property Binding Example
export class StudentComponent {
studentName = 'Sam';
isDisabled = false;
imageUrl = 'assets/images/student.png';
}
<div>
<h2>
{{ studentName }}
</h2>
<input
[value]="studentName"
type="text">
<button
[disabled]="isDisabled">
Submit
</button>
<img
[src]="imageUrl"
alt="Student">
</div>
Student Profile
Name: Sam
Best Practices
Use square bracket syntax when dynamically setting an element property.
Avoid complex logic directly inside property bindings.
When you only need to display text, interpolation is often the clearer choice.
Clear component property names make bindings easier to understand.
Add bindings only where the UI actually depends on component data.
Summary
- Property binding connects component data with element properties.
- It uses square bracket syntax such as
[disabled]. - It can dynamically control element states and values.
- Property binding can be used with HTML elements and components.
- Class and style properties can also be dynamically bound.
- Property binding is a one-way data binding technique.