Home / Angular / Property Binding in Angular 18: Step by Step Guide with Example

Property Binding in Angular 18: Step by Step Guide with Example

Last updated on January 11th, 2025

Angular is a popular framework for building dynamic and interactive web applications. One of the core features that makes Angular powerful is data binding, which allows seamless communication between components and the DOM. Among the various types of data binding, property binding plays a crucial role in connecting component data to DOM properties.

In this article, we will explore property binding in Angular 18 in detail.

What is Property Binding in Angular?

Property binding is a one-way data-binding technique in Angular that binds data from the component class to the DOM properties of an element. It enables dynamic updates in the DOM whenever the component data changes.

Syntax of Property Binding

The syntax for property binding is:

[DOM_property]="component_property"

  • DOM_property: A property of the HTML element (e.g., src, disabled, value).
  • component_property: A property or method of the Angular component.

For example, to bind a disabled attribute to a button:

button [disabled]="isButtonDisabled">Click Me</button>

Here, the isButtonDisabled property in the component determines whether the button is disabled.

Setting Up an Angular Project

rerequisites for Setting Up an Angular Project

1. Install Node.js and npm

  • Angular requires Node.js and its package manager, npm.
  • Download and Install Node.js:
    • Visit Node.js and download the LTS version.
    • npm comes bundled with Node.js.

2. Install Angular CLI

  • Angular CLI is the official command-line interface to create, build, and manage Angular projects.
  • Install Angular CLI globally using npm:bashCopy codenpm install -g @angular/cli
  • Verify the installation:bashCopy codeng version
npm install -g @angular/cli

3. Install a Code Editor

If you are new to angular refer Angular project Setup Step by Step Practical Tutorial – Full Stack Tutorials Hub .

4.Create a New Angular Project

Run the following command to create a new project named property-binding-demo:

ng new property-binding-demo

F

Follow the prompts to set up your project. Navigate to the project directory:

cd property-binding-demo

5.Generate a Component

Generate a component named customer-form using:

ng generate component customer-form

This creates the component files under src/app/customer-form.

Learn more about How to create custom component without using angular CLI How to Create and Use Your Own Components in Angular 17 – No CLI – Full Stack Tutorials Hub.

Building a Customer Form with Property Binding

We will build a simple customer form that uses property binding for dynamic form behavior.

Update customer-form.component.ts

Modify the component class to include properties for form fields and a button state:

import { Component } from '@angular/core';

@Component({
  selector: 'app-customer-form',
  templateUrl: './customer-form.component.html',
  styleUrls: ['./customer-form.component.css']
})
export class CustomerFormComponent {
  customerName: string = '';
  customerEmail: string = '';
  isSubmitDisabled: boolean = true;

  onInputChange(): void {
    // Enable the submit button if both fields have values
    this.isSubmitDisabled = !(this.customerName && this.customerEmail);
  }
}

Update customer-form.component.html

Use property binding for dynamic behavior in the form:

<div class="form-container">
  <h2>Customer Form</h2>
  <form>
    <label for="name">Name:</label>
    <input
      type="text"
      id="name"
      [(ngModel)]="customerName"
      (input)="onInputChange()"
      placeholder="Enter your name"
    />

    <label for="email">Email:</label>
    <input
      type="email"
      id="email"
      [(ngModel)]="customerEmail"
      (input)="onInputChange()"
      placeholder="Enter your email"
    />

    <button [disabled]="isSubmitDisabled">Submit</button>
  </form>
</div>

Update customer-form.component.css

Add basic styling for the form:

.form-container {
  width: 300px;
  margin: 0 auto;
  padding: 20px;
  border: 1px solid #ccc;
  border-radius: 5px;
}

label {
  display: block;
  margin-bottom: 5px;
  font-weight: bold;
}

input {
  width: 100%;
  padding: 8px;
  margin-bottom: 15px;
  border: 1px solid #ccc;
  border-radius: 3px;
}

button {
  width: 100%;
  padding: 10px;
  background-color: #007bff;
  color: #fff;
  border: none;
  border-radius: 3px;
  cursor: pointer;
}

button:disabled {
  background-color: #ccc;
  cursor: not-allowed;
}

Run the Application

Start the development server:

ng serve

Visit http://localhost:4200 in your browser. Navigate to the customer-form component to see the form in action.

Understanding Property Binding in the Example

  1. Dynamic Button State:
    • The isSubmitDisabled property dynamically enables or disables the submit button using [disabled] property binding.
  2. Two-Way Binding:
    • The [(ngModel)] directive binds input values to component properties, and the (input) event triggers the onInputChange() method to update the button state.
  3. Reactivity:
    • The DOM updates instantly when component properties change, providing a responsive user experience.

External References

FAQ

Conclusion

Property binding in Angular 18 is a powerful feature that enables developers to create dynamic and responsive web applications. By understanding the concepts and applying them in real-world scenarios like the customer form example, you can leverage Angular’s potential to build interactive UIs efficiently. Explore the references and FAQs to deepen your understanding of property binding.

Scroll to Top