Data Binding Between Components
Introduction
Welcome to the very first lesson of the Building Components in Angular course! 🎉 In this lesson, we will delve into the concept of data binding between components, a fundamental aspect of Angular applications. Components are the building blocks of Angular, encapsulating the logic, template, and styles needed to render a part of the user interface. Understanding how components communicate through data binding is crucial for building dynamic and interactive web applications. By the end of this lesson, you'll be equipped with the knowledge to implement data binding using the @Input and @Output decorators, setting a strong foundation for your Angular journey.
Recap: Components and Data Binding
Angular components are the core elements of Angular applications, encapsulating the logic, template, and styles needed to render a part of the user interface. Each component consists of a TypeScript class, an HTML template, and optional CSS styles. For example:
In this example, the @Component decorator defines the component, with selector specifying the custom HTML tag, template containing the HTML to be rendered, and styles defining the CSS styling. The ExampleComponent class holds the component's logic.
Data binding is a powerful feature in Angular that allows components to communicate and share data, ensuring that changes in the data model are reflected in the view and vice versa. There are two main types of data binding: one-way and two-way. One-way data binding flows data in a single direction, either from the component to the view or from the view to the component. Two-way data binding allows for a bidirectional flow of data. Here's a simple example of one-way data binding:
In this example, the {{ message }} syntax binds the message property from the component to the view, automatically updating the displayed text when message changes.
Using @Input for Parent-to-Child Communication
The @Input decorator is used to pass data from a parent component to a child component. This is essential for creating dynamic and reusable components. Let's see how it works step-by-step:
-
Define the Child Component: Start by creating a child component with an
@Inputproperty.TypeScriptHere, the
ChildComponenthas an@Inputproperty namedtitle. This allows the parent component to pass a value to it. -
Use the Child Component in the Parent: In the parent component, use the child component and bind a value to the
titleproperty.HTMLThe
[title]="parentTitle"syntax binds theparentTitleproperty from the parent component to thetitleproperty of the child component.
When a parent component passes a value to a child component via @Input, Angular checks whether the reference to the input property has changed. If a primitive value (like a string or number) is updated, Angular detects the change automatically. However, if an object or array is passed and modified without changing its reference, Angular does not trigger change detection. For this, you would need to assign a new object reference: this.user = { ...this.user, name: 'newName' };
