


How do Angular components communicate? 2 methods for parent-child component communication
This article will take you to understand the component communication in Angular, and introduce the methods of communication between parent and child components, and communication between components that have no direct relationship.
In actual applications, our components will be related in a tree structure, so the relationship between components is mainly:
【 Related tutorial recommendations: "angular tutorial"】
Father-son relationship
Brother relationship
No direct relationship
Prepare our environment:
1. Create a header
Components: ng g c components/header
1 2 3 |
|
1 2 3 4 5 6 |
|
2. Create a title
component: ng g c components/title
1 |
|
1 2 3 4 5 6 7 8 |
|
3. Create a button
component: ng g c components/button
1 |
|
1 2 3 4 5 6 7 |
|
Directly call
Applies to parent-child relationship components. Note that direct calls make the coupling of parent-child components higher. To make it clear, direct calls are required.
1. Mount our header component into the app so that a parent-child component relationship is formed between the app and the header.
2. Use # for us Give the component a name:
<app-header #header></app-header>
3. Now our header component is still very empty, let’s expand it, otherwise What to call?
1 2 3 4 5 6 7 |
|
4. After the component is expanded, we can call the properties and functions in the sub-component header in the parent component app
1 2 3 4 5 |
|
5. The fourth step is to The operation is performed in the html template of the component. Sometimes we also need to operate the sub-component in the ts class of the parent component. We will demonstrate next.
6. We need to use a new decorator@ViewChild(Component)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
@Input and @Output
Applicable to parent-child relationship components
1. We solve this problem by defining title
in the header
component. Direct calls in the coupled title
component lead to complex expansion problems
2. Add @Input() decoration to the title
attribute in the title
component Container: @Input() public title: string = 'Title';
3. Add a title attribute to the header component and assign a value: public title: string = 'I am New title';
4. We use the title
component in the html
template of the header
component like this: <app-title [title]="title"></app-title>
5. Let’s take a look at the effect so far. Although the interface is ugly, I will use the component next time Is it more convenient to set title
?
6. The above steps realize that the data of the parent component is passed to the child component, then let’s continue Let’s see how the data of the child component is passed to the parent component? Let’s use the @Output()
decorator to implement the following
7. In the title
component Add the titleChange
attribute to the ts class: @Output() public titleChange = new EventEmitter();
8. In the ts of the title
component Regularly dispatch data in the class
1 2 3 4 5 6 |
|
9. Now let’s modify the header parent component to receive the dispatched data:
1 2 3 4 |
|
1 2 3 |
|
Use service simple interest for communication
Applicable to components that have no direct relationship
1. Since we need to communicate through services, we first Create a service: ng g s services/EventBus
, and we declare an attribute of type Subject
to assist communication
1 2 3 4 5 6 7 8 |
|
2. To save trouble, we will not The components have been re-created, because the button component and title component in our header
are components that have no direct relationship.
3. Transform our button
component and add a click event to trigger the triggerEventBus
function
1 2 3 4 5 6 7 8 9 10 11 |
|
4. In title
Acquisition of simulated data in components
1 2 3 4 5 6 7 8 9 10 |
|
Use cookie, session or localstorage to communicate
1. This is very simple. We still use the title
component and the button
component for demonstration. This time we save the data in the title component. In Get data from the button
component. Let’s only demonstrate localstorage
, everything else is the same.
2. Save title
in the ngOnInit()
hook of the title
component to localstorage
: window.localStorage.setItem('title', this.title);
3. Get data in the button component: const title = window.localStorage.getItem('title');
Conclusion:
In this article we have introduced Angular’s component communication, so that our split components can perform reasonable Communication provides guarantee, and the use of components until now is done by introducing tags.
Original address: https://juejin.cn/post/6991471300837572638
Author: Xiaoxin
For more programming-related knowledge, please Visit: Introduction to Programming! !
The above is the detailed content of How do Angular components communicate? 2 methods for parent-child component communication. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Angular.js is a freely accessible JavaScript platform for creating dynamic applications. It allows you to express various aspects of your application quickly and clearly by extending the syntax of HTML as a template language. Angular.js provides a range of tools to help you write, update and test your code. Additionally, it provides many features such as routing and form management. This guide will discuss how to install Angular on Ubuntu24. First, you need to install Node.js. Node.js is a JavaScript running environment based on the ChromeV8 engine that allows you to run JavaScript code on the server side. To be in Ub

This article will give you an in-depth understanding of Angular's state manager NgRx and introduce how to use NgRx. I hope it will be helpful to you!

Do you know Angular Universal? It can help the website provide better SEO support!

With the rapid development of the Internet, front-end development technology is also constantly improving and iterating. PHP and Angular are two technologies widely used in front-end development. PHP is a server-side scripting language that can handle tasks such as processing forms, generating dynamic pages, and managing access permissions. Angular is a JavaScript framework that can be used to develop single-page applications and build componentized web applications. This article will introduce how to use PHP and Angular for front-end development, and how to combine them

How to use monaco-editor in angular? The following article records the use of monaco-editor in angular that was used in a recent business. I hope it will be helpful to everyone!

This article will take you through the independent components in Angular, how to create an independent component in Angular, and how to import existing modules into the independent component. I hope it will be helpful to you!

The default display behavior for components in the Angular framework is not for block-level elements. This design choice promotes encapsulation of component styles and encourages developers to consciously define how each component is displayed. By explicitly setting the CSS property display, the display of Angular components can be fully controlled to achieve the desired layout and responsiveness.

The Angular project is too large, how to split it reasonably? The following article will introduce to you how to reasonably split Angular projects. I hope it will be helpful to you!
